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 ex1/ex1
Binary file not shown.
18 changes: 17 additions & 1 deletion ex1/ex1.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@
// (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process?
// What happens to the variable when both the child and parent change the value of x?

// In the program below, the parent process decrements and the child process increments. Because they are two separate processes running the
// code independently, x = 99 for the parent and x = 101 for the child.

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

int main(void)
{
// Your code here
int x = 100;
int pid = fork();
if (pid < 0) {
fprintf(stderr, "fork failed\n");
exit(1);
}

if (pid == 0) {
x++;
}
else {
x--;
}
printf("%d, %s, %d\n", pid, "hello", x);

return 0;
}
Binary file added ex2/ex2
Binary file not shown.
19 changes: 19 additions & 0 deletions ex2/ex2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@
// 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?

// Both the child and the parent can access the file descriptor, if both processes attempt to write to the file then
// both attempts will be saved.

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

int main(void)
{
FILE *fp;

fp = fopen("text.txt", "r+");

int pid = fork();

if (pid == 0) {
fprintf(fp, "%s %s %s %s", "The", "child", "wrote", "this.");

fclose(fp);
}
else {
fprintf(fp, "%s %s %s %s", "The", "parent", "wrote", "this.");

fclose(fp);
}
// Your code here

return 0;
Expand Down
1 change: 1 addition & 0 deletions ex2/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The parent wrote this.The child wrote this.
Binary file added ex3/ex3
Binary file not shown.
9 changes: 8 additions & 1 deletion ex3/ex3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

int main(void)
{
// Your code here
int pid = fork();
waitpid(pid, NULL, 0);
if (pid == 0) {
printf("%s\n", "hello");
}
else {
printf("%s\n", "goodbye");
}

return 0;
}
Binary file added ex4/ex4
Binary file not shown.
4 changes: 3 additions & 1 deletion ex4/ex4.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

int main(void)
{
// Your code here
char *args[]={"/bin/ls", NULL};
fork();
execv(args[0], args);

return 0;
}
Binary file added ex5/a.out
Binary file not shown.
Binary file added ex5/ex5
Binary file not shown.
27 changes: 25 additions & 2 deletions ex5/ex5.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,30 @@ char* msg3 = "hello world #3";

int main(void)
{
// Your code here

char inbuf[MSGSIZE];
int p[2];
if (pipe(p) < 0) {
fprintf(stderr, "pipe failed\n");
exit(1);
}
int pid = fork();

if (pid < 0) {
fprintf(stderr, "fork failed\n");
exit(1);
}

if (pid == 0) {
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
}
else {
for (int i = 0; i < 3; i++) {
// read 16 bytes of data from the read file descriptor
read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}
}
return 0;
}
Binary file added ex6/ex6
Binary file not shown.
15 changes: 14 additions & 1 deletion ex6/ex6.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and `clock_gettime()` should work just fine.
*/

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>

Expand All @@ -20,7 +21,19 @@ and `clock_gettime()` should work just fine.

int main()
{
// Your code here
uint64_t diff;
struct timespec start, end;
int i;

clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < number_iter; i++)
{
write(fileno(stdout), NULL, 0);
}
clock_gettime(CLOCK_MONOTONIC, &end);

diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time = %llu nanoseconds\n", (long long unsigned int) diff);

return 0;
}
1 change: 1 addition & 0 deletions stretch/src/balance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8
Binary file added stretch/src/bankers
Binary file not shown.
51 changes: 39 additions & 12 deletions stretch/src/bankers.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ void read_balance(int fd, int *balance)
*/
int get_random_amount(void)
{
// vvvvvvvvvvvvvvvvvv
// !!!! IMPLEMENT ME:

// Return a random number between 0 and 999 inclusive using rand()

// ^^^^^^^^^^^^^^^^^^
return rand() % 1000;
}

/**
Expand All @@ -116,16 +111,27 @@ int main(int argc, char **argv)
// message to stderr, and exit with status 1:
//
// "usage: bankers numprocesses\n"
if (argc < 2)
{
fprintf(stderr, "usage: bankers numprocessess\n");
exit(1);
}

// Store the number of processes in this variable:
// How many processes to fork at once
int num_processes = IMPLEMENT ME
int num_processes = strtol(argv[1], NULL, 10);

// Make sure the number of processes the user specified is more than
// 0 and print an error to stderr if not, then exit with status 2:
//
// "bankers: num processes must be greater than 0\n"

if (num_processes < 1)
{
fprintf(stderr, "bankers: num processes must be greater than 0\n");
exit(2);
}

// ^^^^^^^^^^^^^^^^^^

// Start with $10K in the bank. Easy peasy.
Expand All @@ -145,15 +151,36 @@ int main(int argc, char **argv)
int amount = get_random_amount();

int balance;

// vvvvvvvvvvvvvvvvvvvvvvvvv
// !!!! IMPLEMENT ME

fd = open_balance_file(BALANCE_FILE);
int lock = flock(fd, LOCK_EX);
if (lock < 0)
{
fprintf(stderr, "lock failed\n");
exit(1);
}
// Open the balance file (feel free to call the helper
// functions, above).


read_balance(fd, &balance);
// Read the current balance

if (balance > amount)
{
int new_balance = balance - amount;
write_balance(fd, new_balance);
printf("Withdrew $%d, new balance $%d\n", amount, new_balance);
}
else
{
printf("Only have $%d, can't withdraw $%d\n", balance, amount);
}
int unlock = flock(fd, LOCK_UN);
if (unlock < 0) {
fprintf(stderr, "unlock failed\n");
exit(1);
}

// Try to withdraw money
//
// Sample messages to print:
Expand All @@ -163,7 +190,7 @@ int main(int argc, char **argv)

// Close the balance file
//^^^^^^^^^^^^^^^^^^^^^^^^^^

close_balance_file(fd);
// Child process exits
exit(0);
}
Expand Down