Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hargobind Atwal #393

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>

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;
}
53 changes: 53 additions & 0 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,63 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

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;
}
13 changes: 13 additions & 0 deletions ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 19 additions & 1 deletion ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
48 changes: 44 additions & 4 deletions ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,54 @@
#include <sys/wait.h>

#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;
}
}
29 changes: 29 additions & 0 deletions ex5/pipe3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
** pipe3.c -- a smartest pipe example
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

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;
}

20 changes: 20 additions & 0 deletions ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,33 @@ and `clock_gettime()` should work just fine.
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h> /* 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;
}
2 changes: 2 additions & 0 deletions runner.sh
Original file line number Diff line number Diff line change
@@ -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"