├── README.md ├── dup_fonksiyonu_kullanimi.c ├── execv_fonksiyonu_kullanimi.c ├── execl_fonksiyonunun_kullanmasi.c ├── execv2_fonksiyonu_kullanimi.c ├── execv3_fonksiyonu_kullanimi.c ├── dosya_betimleyici_tablo_uzunlugu.c ├── ust_proses.c ├── stdout_dosyasinin_yonlendirme.c ├── dup2_fonksiyonu_ile_io_yonlendirmesi.c └── alt_proseste_io_yonlendirmesi.c /README.md: -------------------------------------------------------------------------------- 1 | linux-system-programming 2 | ======================== 3 | 4 | Unix/Linux System Programming 5 | -------------------------------------------------------------------------------- /dup_fonksiyonu_kullanimi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void exitsys(const char *msg); 7 | int main(int argc, char *argv[]) 8 | { 9 | int fdnew; 10 | 11 | if ((fdnew = dup(1)) < 0) 12 | exitsys("dup"); 13 | write(fdnew, "test\n", 5); 14 | close(fdnew); 15 | return 0; 16 | } 17 | 18 | void exitsys(const char *msg) 19 | { 20 | perror(msg); 21 | exit(EXIT_FAILURE); 22 | } 23 | -------------------------------------------------------------------------------- /execv_fonksiyonu_kullanimi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void exitsys(const char *msg); 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | char *args[] = {"/bin/ls", "-l", "-i", NULL}; 11 | 12 | if (execv("/bin/ls", args) < 0) 13 | exitsys("execl"); 14 | 15 | printf("unreachable code!..\n"); 16 | 17 | return 0; 18 | } 19 | 20 | void exitsys(const char *msg) 21 | { 22 | perror(msg); 23 | exit(EXIT_FAILURE); 24 | } 25 | -------------------------------------------------------------------------------- /execl_fonksiyonunun_kullanmasi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void exitsys(const char *msg); 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | printf("starts...\n"); 11 | 12 | if (execl("/bin/ls", "/bin/ls", "-l", (char *) NULL) < 0) 13 | exitsys("execl"); 14 | 15 | printf("unreachable code!..\n"); 16 | 17 | return 0; 18 | } 19 | 20 | void exitsys(const char *msg) 21 | { 22 | perror(msg); 23 | exit(EXIT_FAILURE); 24 | } 25 | -------------------------------------------------------------------------------- /execv2_fonksiyonu_kullanimi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void exitsys(const char *msg); 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | if (argc < 2) { 11 | fprintf(stderr, "wrong number of arguments!..\n"); 12 | exit(EXIT_FAILURE); 13 | } 14 | 15 | if (execv(argv[1], &argv[1]) < 0) 16 | exitsys("execl"); 17 | 18 | printf("unreachable code!..\n"); 19 | 20 | return 0; 21 | } 22 | 23 | void exitsys(const char *msg) 24 | { 25 | perror(msg); 26 | exit(EXIT_FAILURE); 27 | } 28 | -------------------------------------------------------------------------------- /execv3_fonksiyonu_kullanimi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void exitsys(const char *msg); 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | if (argc < 2) { 11 | fprintf(stderr, "wrong number of arguments!..\n"); 12 | exit(EXIT_FAILURE); 13 | } 14 | 15 | if (execp(argv[1], &argv[1]) < 0) 16 | exitsys("execl"); 17 | 18 | printf("unreachable code!..\n"); 19 | 20 | return 0; 21 | } 22 | 23 | void exitsys(const char *msg) 24 | { 25 | perror(msg); 26 | exit(EXIT_FAILURE); 27 | } 28 | -------------------------------------------------------------------------------- /dosya_betimleyici_tablo_uzunlugu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void exitsys(const char *msg); 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | int fd; 12 | 13 | for (i = 1;; ++i) { 14 | if ((fd = open(argv[1], O_RDONLY)) < 0) 15 | break; 16 | printf("count = %d, fd = %d\n", i, fd); 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | void exitsys(const char *msg) 23 | { 24 | perror(msg); 25 | exit(EXIT_FAILURE); 26 | } 27 | -------------------------------------------------------------------------------- /ust_proses.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void exitsys(const char *msg); 7 | 8 | int g_x; 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | pid_t pid; 13 | 14 | if ((pid = fork()) < 0) 15 | exitsys("fork"); 16 | 17 | if (pid != 0) { 18 | g_x = 10; 19 | printf("parent process...\n"); 20 | 21 | } 22 | else { 23 | sleep(2); 24 | printf("child process: %d\n", g_x); /* 0 yazdýrýlacak */ 25 | } 26 | 27 | printf("ends...\n"); 28 | 29 | return 0; 30 | } 31 | 32 | void exitsys(const char *msg) 33 | { 34 | perror(msg); 35 | exit(EXIT_FAILURE); 36 | } 37 | -------------------------------------------------------------------------------- /stdout_dosyasinin_yonlendirme.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void exitsys(const char *msg); 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int fd; 13 | int i; 14 | close(1); 15 | 16 | if ((fd = open("a.txt", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)) < 0) 17 | exitsys("open"); 18 | 19 | for (i = 0; i < 100; ++i) 20 | printf("%d\n", i); 21 | return 0; 22 | } 23 | 24 | void exitsys(const char *msg) 25 | { 26 | perror(msg); 27 | exit(EXIT_FAILURE); 28 | } 29 | -------------------------------------------------------------------------------- /dup2_fonksiyonu_ile_io_yonlendirmesi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void exitsys(const char *msg); 9 | int main(int argc, char *argv[]) 10 | { 11 | int fd; 12 | int i; 13 | 14 | if ((fd = open("test.txt", O_WRONLY|O_CREAT, 15 | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) 16 | exitsys("open"); 17 | 18 | if (dup2(fd, STDOUT_FILENO) < 0) 19 | exitsys("dup2"); 20 | for (i = 0; i < 10; ++i) 21 | printf("%d\n", i); 22 | 23 | close(fd); 24 | return 0; 25 | } 26 | 27 | void exitsys(const char *msg) 28 | { 29 | perror(msg); 30 | exit(EXIT_FAILURE); 31 | } 32 | -------------------------------------------------------------------------------- /alt_proseste_io_yonlendirmesi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void exitsys(const char *msg); 9 | 10 | /* Usage: direct */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | pid_t pid; 15 | int fd; 16 | 17 | if (argc < 3) { 18 | fprintf(stderr, "wrong number of arguments!..\n"); 19 | 20 | exit(EXIT_FAILURE); 21 | 22 | } 23 | 24 | if ((pid = fork()) < 0) 25 | exitsys("fork"); 26 | 27 | if (pid == 0) { 28 | if ((fd = open(argv[1], O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IRGRP)) < 0) 29 | exitsys("open"); 30 | if (dup2(fd, STDOUT_FILENO) < 0) 31 | exitsys("dup2"); 32 | 33 | close(fd); 34 | 35 | if (execvp(argv[2], &argv[2]) < 0) 36 | exitsys("execvp"); 37 | 38 | /* unreachable code */ 39 | 40 | } 41 | 42 | if (waitpid(pid, NULL, 0) < 0) 43 | exitsys("waitpid"); 44 | 45 | return 0; 46 | 47 | } 48 | 49 | void exitsys(const char *msg) 50 | { 51 | perror(msg); 52 | exit(EXIT_FAILURE); 53 | } 54 | --------------------------------------------------------------------------------