diff --git a/ex1/ex1.c b/ex1/ex1.c index c4b111641..278651b7d 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(1); + } + else + { + printf("%d Parent processes 1 %d\n", *x, pid); + (*x)++; + printf("%d Parent processes 2 %d\n", *x, pid); + waitpid(pid, 1, 1); + (*x)++; + printf("%d Parent processes 3 %d\n", *x, pid); + } return 0; } diff --git a/ex2/ex2.c b/ex2/ex2.c index 4245375b9..3ab33f12f 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -5,10 +5,63 @@ #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"); + // 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"); + + } + + char buff[128] = {0}; + + // fgets(buff, 128, fp); + read(fp, buff, 100); + + 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/ex4/ex4.c b/ex4/ex4.c index 0221ca96e..f63011854 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -10,7 +10,25 @@ int main(void) { - // Your code here + // Your code here + 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 + { + execle("/bin/cd ex4", "cd", NULL); + execle("/bin/ls", "ls", NULL); + // exec("/bin/ls"); + } + else // parent process + { + execle("/bin/ls", "ls", NULL); + + } return 0; } diff --git a/ex5/ex5.c b/ex5/ex5.c index cbf3b8e61..af0960e1d 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -9,14 +9,54 @@ #include #define MSGSIZE 16 - +#define BUFF_SIZE 128 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 - + + char buf[128]; + + // fd[0] READ end of the pipe + // fd[1] WRITE end of the pipe + int fd[2]; // file descriptor + + pipe(fd); + + 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]); + + // 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; -} +} \ No newline at end of file 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; +} + diff --git a/ex6/ex6.c b/ex6/ex6.c index 17532d65f..0acfc3d7f 100644 --- a/ex6/ex6.c +++ b/ex6/ex6.c @@ -14,13 +14,33 @@ 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); + 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 %llu \n",(long long unsigned int) diff); 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"