Skip to content
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
Binary file added .vscode/ipch/d017f139b4fd457c/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/da6cf284263e2cc/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/de4ae1eb1c013c04/EX2.ipch
Binary file not shown.
Binary file added .vscode/ipch/de4ae1eb1c013c04/mmap_address.bin
Binary file not shown.
Binary file added ex1/ex1
Binary file not shown.
14 changes: 14 additions & 0 deletions ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,19 @@ int main(void)
{
// Your code here

pid_t pid = fork();

if (pid == 0)
{
int x = 200;
printf("\n This is the child, x = %d\n", x);
exit(1);
}
else
{
int x = 100;
printf("\n This is the parent, x = %d\n", x);
}

return 0;
}
Binary file added ex2/ex2
Binary file not shown.
24 changes: 20 additions & 4 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// Write a program that opens the text.txt file (with the `fopen()` library call) located in this directory
// and then calls `fork()` to create a new process. Can both the child and parent access the file descriptor
// returned by `fopen()`? What happens when they are written to the file concurrently?

#include <stdio.h>
Expand All @@ -8,7 +8,23 @@

int main(void)
{
// Your code here

// Your code here
pid_t pid = fork();
FILE *fp;
fp = fopen("text.txt", "w+");

if (pid == 0)
{

fprintf(fp, "\n I\'m printing from the child\n");
exit(1);
}
else
{
fprintf(fp, "\n I\'m printing from the parent\n");
}

return 0;
}

// oddly enough only the child printed in my code.. did I do something wrong?
3 changes: 3 additions & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

I'm printing from the child

Binary file added ex3/ex3
Binary file not shown.
12 changes: 12 additions & 0 deletions ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
int main(void)
{
// Your code here
pid_t pid = fork();

if (pid == 0)
{
printf("\nHello\n");
exit(1);
}
else
{
wait(NULL);
printf("\nGoodbye\n");
}

return 0;
}
Binary file added ex4/ex4
Binary file not shown.
14 changes: 14 additions & 0 deletions ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
int main(void)
{
// Your code here
pid_t pid = fork();

if (pid == 0) {
// execlp("ls", "ls", NULL);
char *cmd = "ls";
char *argv[3];
argv[0] = "ls";
argv[1] = "-la";
argv[2] = NULL;

execvp(cmd, argv); //This will run "ls -la" as if it were a command
} else {
wait(NULL);
printf("Child process has ended\n");
}
return 0;
}
Binary file added ex5/ex5
Binary file not shown.
31 changes: 28 additions & 3 deletions ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,38 @@

#define MSGSIZE 16

char* msg1 = "hello world #1";
char* msg2 = "hello world #2";
char* msg3 = "hello world #3";
char* msg1 = "hello world #1\n";
char* msg2 = "hello world #2\n";
char* msg3 = "hello world #3\n";

int main(void)
{
// Your code here
int fd[2]; // init pipe array
pipe(fd); // init the pipe

pid_t pid = fork(); // init the fork


char* messages[sizeof MSGSIZE];
int buf[128];
messages[0] = msg1;
messages[1] = msg2;
messages[2] = msg3;

if (pid < 0) {
perror("fork failed\n");
exit(1);
} else if (pid == 0) {
write(fd[1], messages[0], sizeof messages[0]);
write(fd[1], messages[1], sizeof messages[1]);
write(fd[1], messages[2], sizeof messages[2]);
} else {
wait(NULL);
read(fd[0], messages[0], sizeof buf);
read(fd[0], messages[1], sizeof buf);
read(fd[0], messages[2], sizeof buf);
}

return 0;
}