From 225f561051072f8aceb6837cb5b6bd127ecf3279 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Wed, 27 Mar 2019 13:10:18 -0700 Subject: [PATCH 1/8] exploring fork --- ex1/ex1.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..d0e2d86cf 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -5,10 +5,36 @@ #include #include #include +#include +#include int main(void) { // Your code here + int *x = malloc(sizeof(int)); + pid_t pid = fork(); + (*x)++; + if (pid == 0) + { + printf("%d Child proccess 1 %d\n", *x, pid); + (*x)++; + printf("%d Child proccess 2 %d\n", *x, pid); + + execlp("ls", "ls", NULL); + perror("exec"); + (*x)++; + printf("%d Child proccess 3 %d\n", *x, pid); + // exit(9); + } + else + { + printf("%d Parent processes 1 %d\n", *x, pid); + (*x)++; + printf("%d Parent processes 2 %d\n", *x, pid); + wait(0); + (*x)++; + printf("%d Parent processes 3 %d\n", *x, pid); + } return 0; } From 023615a93a8c1e139f956f339aa190448447aa82 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Wed, 27 Mar 2019 13:14:26 -0700 Subject: [PATCH 2/8] exploring fork --- ex1/ex1.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..d0e2d86cf 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -5,10 +5,36 @@ #include #include #include +#include +#include int main(void) { // Your code here + int *x = malloc(sizeof(int)); + pid_t pid = fork(); + (*x)++; + if (pid == 0) + { + printf("%d Child proccess 1 %d\n", *x, pid); + (*x)++; + printf("%d Child proccess 2 %d\n", *x, pid); + + execlp("ls", "ls", NULL); + perror("exec"); + (*x)++; + printf("%d Child proccess 3 %d\n", *x, pid); + // exit(9); + } + else + { + printf("%d Parent processes 1 %d\n", *x, pid); + (*x)++; + printf("%d Parent processes 2 %d\n", *x, pid); + wait(0); + (*x)++; + printf("%d Parent processes 3 %d\n", *x, pid); + } return 0; } From d6ec6b32be2d646982ef4727b45782de64cf6e35 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Wed, 27 Mar 2019 14:00:04 -0700 Subject: [PATCH 3/8] got through ex1 - ex3 --- ex2/ex2.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ ex3/ex3.c | 13 +++++++++++++ runner.sh | 2 ++ 3 files changed, 66 insertions(+) create mode 100755 runner.sh diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..c53532159 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -5,10 +5,61 @@ #include #include #include +#include +#include int main(void) { // Your code here + + // open file + FILE * fp = fopen("text.txt", "w+"); + if (!fp) { + perror("File opening failed"); + return EXIT_FAILURE; + } + mkdir("subdir", 0755); + + + // read file loop + int c; + while((c = fgetc(fp)) != EOF) + { + putchar(c); + } + + // writing to file from parent and child process + + fprintf(fp, "this came from the program\n"); + + pid_t pid = fork(); + if (pid == 0) // Child process + { + + fprintf(fp, "this came from the Child process\n"); + + } + else // Parrent process + { + wait(NULL); + fprintf(fp, "this came from the Parent process\n"); + + } + + char buff[128]; + + fgets(buff, 128, fp); + + printf("Buffer has: \"%s\n\"", buff); + + + if (ferror(fp)) + puts("I/O error when reading"); + else if (feof(fp)) + puts("End of file reached successfully"); + + fclose(fp); + return 0; } diff --git a/ex3/ex3.c b/ex3/ex3.c index 3a3698c1f..bdc8e67e1 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -11,5 +11,18 @@ int main(void) { // Your code here + pid_t pid = fork(); + if (pid == 0) // Child process + { + printf("Hello\n"); + } + else // Parrent process + { + wait(NULL); + printf("goodbye\n"); + + } + + return 0; } diff --git a/runner.sh b/runner.sh new file mode 100755 index 000000000..52c69b9c6 --- /dev/null +++ b/runner.sh @@ -0,0 +1,2 @@ +#gcc $1/$1.c -o $1/$1 && ./$1/$1 +nodemon -w $1/$1.c --exec "gcc $1/$1.c -o $1/$1 && ./$1/$1" From acc0e0b0e63d9a04bd60b73ad14c128a58936827 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Wed, 27 Mar 2019 14:37:50 -0700 Subject: [PATCH 4/8] experimenting with sys calls --- ex1/ex1.c | 4 ++-- ex4/ex4.c | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ex1/ex1.c b/ex1/ex1.c index d0e2d86cf..278651b7d 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -25,14 +25,14 @@ int main(void) (*x)++; printf("%d Child proccess 3 %d\n", *x, pid); - // exit(9); + // exit(1); } else { printf("%d Parent processes 1 %d\n", *x, pid); (*x)++; printf("%d Parent processes 2 %d\n", *x, pid); - wait(0); + waitpid(pid, 1, 1); (*x)++; printf("%d Parent processes 3 %d\n", *x, pid); } diff --git a/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..66e125c9e 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,23 @@ int main(void) { - // Your code here + // Your code here + + // execlp("/bin/ls", "ls", NULL); + pid_t pid = fork(); + if (pid == 0) // child process + { + execle("/bin/cd ex4", "cd", NULL); + execle("/bin/ls", "ls", NULL); + // exec("/bin/ls"); + } + else // parent process + { + execle("/bin/ls", "ls", NULL); + + } + + return 0; } From 9ba2522139ef08ea9537ff8d8aae3145904d92bd Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Thu, 28 Mar 2019 12:12:14 -0700 Subject: [PATCH 5/8] some changes --- ex2/ex2.c | 5 +++-- ex4/ex4.c | 12 +++++++----- ex5/ex5.c | 23 +++++++++++++++++++++-- ex5/pipe3.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 ex5/pipe3.c diff --git a/ex2/ex2.c b/ex2/ex2.c index c53532159..4a3acd39f 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -46,9 +46,10 @@ int main(void) } - char buff[128]; + char buff[128] = {0}; - fgets(buff, 128, fp); + // fgets(buff, 128, fp); + read(fp, buff, 100); printf("Buffer has: \"%s\n\"", buff); diff --git a/ex4/ex4.c b/ex4/ex4.c index 66e125c9e..f63011854 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -12,7 +12,12 @@ int main(void) { // Your code here - // execlp("/bin/ls", "ls", NULL); + printf("starting program\n"); + printf("%s", "hello"); + exec("/bin/ls", "ls", NULL); + printf("ran ls\n"); + + execlp("/bin/ls", "ls", NULL); pid_t pid = fork(); if (pid == 0) // child process { @@ -20,13 +25,10 @@ int main(void) execle("/bin/ls", "ls", NULL); // exec("/bin/ls"); } - else // parent process + else // parent process { execle("/bin/ls", "ls", NULL); } - - - return 0; } diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..13e285679 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -9,7 +9,7 @@ #include #define MSGSIZE 16 - +#define BUFF_SIZE 128 char* msg1 = "hello world #1"; char* msg2 = "hello world #2"; char* msg3 = "hello world #3"; @@ -17,6 +17,25 @@ char* msg3 = "hello world #3"; int main(void) { // Your code here - + + char buf[128]; + + // fd[0] READ end of the pipe + // fd[1] WRITE end of the pipe + int fd[2]; // file descriptor + + pipe(fd); + + char inBuff[BUFF_SIZE]; + char aBuff[BUFF_SIZE]; + // fgets(aBuff, BUFF_SIZE, fd[0]); + + int bytes_written = write(fd[1], "We come in peace\n", 17); + + int bytes_read = read(fd[0], buf, sizeof(buf)); + + // write(STDOUT_FILENO, aBuff, bytes_read); + write(STDOUT_FILENO, buf, bytes_read); + return 0; } diff --git a/ex5/pipe3.c b/ex5/pipe3.c new file mode 100644 index 000000000..4058dae66 --- /dev/null +++ b/ex5/pipe3.c @@ -0,0 +1,29 @@ +/* +** pipe3.c -- a smartest pipe example +*/ + +#include +#include +#include + +int main(void) +{ + int pfds[2]; + + pipe(pfds); + + if (!fork()) { + close(1); /* close normal stdout */ + dup(pfds[1]); /* make stdout same as pfds[1] */ + close(pfds[0]); /* we don't need this */ + execlp("ls", "ls", NULL); + } else { + close(0); /* close normal stdin */ + dup(pfds[0]); /* make stdin same as pfds[0] */ + close(pfds[1]); /* we don't need this */ + execlp("wc", "wc", "-l", NULL); + } + + return 0; +} + From 2beebf72a6ea315b456f5f5cb8e44b55668bd812 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Thu, 28 Mar 2019 13:07:58 -0700 Subject: [PATCH 6/8] added process fork --- ex5/ex5.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ex5/ex5.c b/ex5/ex5.c index 13e285679..182b4da6a 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -26,6 +26,13 @@ int main(void) pipe(fd); + if (!fork) { // child process + dup(fd[1]); + } else { // parent process + dup(fd[0]); + + } + char inBuff[BUFF_SIZE]; char aBuff[BUFF_SIZE]; // fgets(aBuff, BUFF_SIZE, fd[0]); From 7f8d1e92cb8f3e87e27f696c0b731fa715f2a6d0 Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Thu, 28 Mar 2019 15:33:56 -0700 Subject: [PATCH 7/8] used clock_gettime --- ex5/ex5.c | 4 ++-- ex6/ex6.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ex5/ex5.c b/ex5/ex5.c index 182b4da6a..0f9252967 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -14,7 +14,7 @@ char* msg1 = "hello world #1"; char* msg2 = "hello world #2"; char* msg3 = "hello world #3"; -int main(void) +int main(int argc, char * argv[]) { // Your code here @@ -45,4 +45,4 @@ int main(void) write(STDOUT_FILENO, buf, bytes_read); return 0; -} +} \ No newline at end of file diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..0904dc841 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -14,13 +14,31 @@ and `clock_gettime()` should work just fine. #include #include #include +#include /* for uint64 definition */ #define number_iter 1000000 #define BILLION 1000000000L +// struct timespec { +// time_t tv_sec; /* seconds */ +// long tv_nsec; /* nanoseconds */ +// }; + + int main() { + uint64_t diff; + struct timespec start, end; + int i; + // Your code here + int start_time = clock_gettime(CLOCK_MONOTONIC, &start); + + sleep(2); + + int end_time = clock_gettime(CLOCK_MONOTONIC, &end); + diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + printf("program ran for %d \n",(long long unsigned int) diff); return 0; } From 2b5d797a03748245c80a6a514b6a516a43df6c5d Mon Sep 17 00:00:00 2001 From: Hargobind Atwal Date: Fri, 29 Mar 2019 12:40:42 -0700 Subject: [PATCH 8/8] got processes to pipe to each other --- ex2/ex2.c | 3 ++- ex5/ex5.c | 32 +++++++++++++++++++++++--------- ex6/ex6.c | 6 ++++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ex2/ex2.c b/ex2/ex2.c index 4a3acd39f..3ab33f12f 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -37,11 +37,12 @@ int main(void) { fprintf(fp, "this came from the Child process\n"); - + // fwrite(fp, "some stuff from child\n"); } else // Parrent process { wait(NULL); + // fwrite(fp, "some stuff from parent\n"); fprintf(fp, "this came from the Parent process\n"); } diff --git a/ex5/ex5.c b/ex5/ex5.c index 0f9252967..af0960e1d 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -26,23 +26,37 @@ int main(int argc, char * argv[]) pipe(fd); - if (!fork) { // child process + if (!fork()) { // child process + close(1); dup(fd[1]); + close(fd[0]); + printf("yooooo"); + write(fd[1], msg1, MSGSIZE); + write(fd[1], msg2, MSGSIZE); + write(fd[1], msg3, MSGSIZE); } else { // parent process + close(0); dup(fd[0]); - + close(fd[1]); + char read_buf[100]; + read(fd[0], read_buf, MSGSIZE); + printf("\"%s\" \n", read_buf); + read(fd[0], read_buf, MSGSIZE); + printf("\"%s\" \n", read_buf); + read(fd[0], read_buf, MSGSIZE); + printf("\"%s\" \n", read_buf); } - char inBuff[BUFF_SIZE]; - char aBuff[BUFF_SIZE]; - // fgets(aBuff, BUFF_SIZE, fd[0]); + // char inBuff[BUFF_SIZE]; + // char aBuff[BUFF_SIZE]; + // // fgets(aBuff, BUFF_SIZE, fd[0]); - int bytes_written = write(fd[1], "We come in peace\n", 17); + // int bytes_written = write(fd[1], "We come in peace\n", 17); - int bytes_read = read(fd[0], buf, sizeof(buf)); + // int bytes_read = read(fd[0], buf, sizeof(buf)); - // write(STDOUT_FILENO, aBuff, bytes_read); - write(STDOUT_FILENO, buf, bytes_read); + // // write(STDOUT_FILENO, aBuff, bytes_read); + // write(STDOUT_FILENO, buf, bytes_read); return 0; } \ No newline at end of file diff --git a/ex6/ex6.c b/ex6/ex6.c index 0904dc841..0acfc3d7f 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -34,11 +34,13 @@ int main() // Your code here int start_time = clock_gettime(CLOCK_MONOTONIC, &start); - sleep(2); + // sleep(2); + char buf[128] = "wasup...\n"; + write(1, buf, 9); int end_time = clock_gettime(CLOCK_MONOTONIC, &end); diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; - printf("program ran for %d \n",(long long unsigned int) diff); + printf("program ran for %llu \n",(long long unsigned int) diff); return 0; }