[Linux] 시스템프로그래밍 기본
Open()
#include <fcntl.h>
#include <stdio.h>
main()
{
char* fname = "test.txt";
int fd;
fd = open(fname, O_RDONLY);
if(fd<0)
perror("open()");
else
{
printf("Success!\nFilename : %10s\nDescriptor : %d\n", fname,fd);
close(fd);
}
}
Read()
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
main()
{
char *fname = "test.txt";
int fd1, fd2, cnt;
char buf[30];
fd1 = open(fname, O_RDONLY);
fd2 = open(fname, O_RDONLY);
if(fd1 <0 || fd2 <0 )
{
perror("open error");
exit(-1);
}
cnt = read(fd1, buf, 12);
buf[cnt] = '\0';
printf("fd1's first printf : %s\n", buf);
lseek(fd1,1,SEEK_CUR);
cnt = read(fd1,buf,12);
buf[cnt] = '\0';
printf("fd1's second printf: %s\n", buf);
cnt = read(fd2, buf, 12);
buf[cnt] = '\0';
printf("fd2's first printf : %s\n", buf);
lseek(fd2,1,SEEK_CUR);
cnt = read(fd2, buf, 12);
buf[cnt] = '\0';
printf("fd2's second printf: %s\n", buf);
exit(0);
}
lseek()
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char* fname = "test.txt";
int fd;
off_t fsize;
if((fd=open(fname, O_RDONLY)) <0 )
{
perror("open()");
exit(-1);
}
fsize = lseek(fd, 0, SEEK_END);
printf("The size of <%s> is %lu bytes.\n", fname,fsize);
exit(0);
}
exec()
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
char *env_init[] = { "USER = unknown", "PATH=/tmp", NULL };
int main()
{
pid_t pid;
if((pid = fork()) <0)
perror("fork error");
else if (pid==0)
{
if(execle("./echoall", "echoall", "myarg1", "My ARG2", (char*)0, env_init) <0 )
perror("execle error");
}
if(waitpid(pid,NULL,0)<0)
perror("wait error");
if((pid = fork()) <0)
perror("fork error");
else if(pid ==0)
{
if(execlp("./echoall", "echoall", "only 1 arg", (char*)0)<0)
perror("execlp error");
}
exit(0);
}
fflush()
#include <stdio.h>
int main()
{
char* prompt = "Prompt>>";
char command[80];
for(;;)
{
fputs(prompt, stdout);
if(fgets(command, sizeof(command), stdin) == NULL)
break;
system(command);
}
fprintf(stdout, "Good Bye ...");
fflush(stdout);
system("clear");
return 0;
}
fork()
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
static int glob = 6;
char buf[] = "a write to stdout\n";
int main()
{
int var;
pid_t pid;
var = 88;
if(write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
perror("write error");
printf("before fork\n");
if((pid = fork())<0)
perror("fork error");
else if(pid==0)
{
glob++;
var++;
}
else
sleep(2);
printf("pid = %d, glob =%d, var = %d\n", getpid(), glob, var);
exit(0);
}
vfork()
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
static int glob = 6;
int main()
{
int var;
pid_t pid;
var = 88;
printf("before vfork\n");
if((pid = vfork())<0)
perror("fork error");
else if(pid==0)
{
glob++;
var++;
printf("pid = %d, glob =%d, var = %d\n", getpid(), glob, var);
_exit(0);
}
else
sleep(2);
printf("pid = %d, glob =%d, var = %d\n", getpid(), glob, var);
exit(0);
}
chdir()
#include <unistd.h>
#include <stdio.h>
#define PATH_MAX 1024
int main(void)
{
char path[PATH_MAX+1];
if(chdir("/tmp") <0)
perror("error chdir");
else
{
if(getcwd(path, PATH_MAX) == NULL)
perror("error getcwd");
else
printf("Current woring directory changed to %s \n", path);
}
return 0;
}
utime()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
int i;
struct stat statbuf;
struct utimbuf timebuf;
char buf[30];
for(i=1; i<argc; i++)
{
if(stat(argv[i], &statbuf) < 0 )
{
sprintf(buf,"%s:stat error", argv[i]);
perror(buf);
continue;
}
if(open(argv[i], O_RDWR | O_TRUNC) <0 )
{
sprintf(buf,"%s:open error", argv[i]);
perror(buf);
continue;
}
timebuf.actime = statbuf.st_atime;
timebuf.modtime = statbuf.st_mtime;
if(utime(argv[i], &timebuf) <0 )
{
sprintf(buf,"%s:utime error", argv[i]);
perror(buf);
continue;
}
}
exit(0);
}
signal
#include <stdio.h>
#include <signal.h>
static void sig_handler(int signo);
int main()
{
int i;
for(i=0; i<100; i++)
{
signal(SIGINT, sig_handler);
printf("[%d]\n",i);
sleep(1);
}
return 0;
}
static void sig_handler(int signo)
{
if(signo == SIGINT)
{
printf(" SIGINT catch !!\n");
SIG_IGN;
}
return;
}