COMPSCI 340 Operating Systems Assignment 1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COMPSCI 340
Operating Systems
Assignment 1 - Fork and threads
Worth 10%
Due Friday the 11th of August at 9:30pm
In this assignment you will investigate the effects of process creation on Linux systems with respect to threads.
You can choose to use either real or virtual Linux including Windows Subsystem for Linux. The first tutorial (starting in week 2) will cover how to set up or access Linux for the assignment.
N.B. Don't use native MacOS because MacOS deals with these situations in a different way.
Part 1
• Read and understand the source code of A1part1.c.
• Answer questions 1 - 2 on the Assignment 1 Questions Canvas page.
• Compile and run the file A1part1.c from a terminal window. On Linux:
gcc -Wall A1part1.c -o A1part1 -pthread
Then run the program with:
./A1part1
• Answer question 3 on Canvas .
In A1part1.c move the call to newProcess before the call to createThreads and then run the program again .
• Answer question 4 on Canvas .
Part 2
• Read the Ubuntu pthread_atfork man page https://manpages.ubuntu.com/manpages/focal/en/ man3/pthread_atfork.3posix.html .
• Answer question 5 on Canvas .
• Write your own program A1part2.c which has a lock (pthread_mutex) and multiple threads.
The program should demonstrate what happens when a process is forked while the lock is still held by one of the other threads (not the thread which calls fork). Is the lock released by the fork or is it not?
• Submit your source code for A1part2.c on the Canvas Assignment 1 Code page.
• Answer question 6 on Canvas .
It is possible that your program will leave a child process running when the program finishes. You need to use the ps command to view processes and then use kill or killall to get rid of extra processes .
Part 3
• Turn your A1part2.c program into another program A1part3.c which uses the pthread_atfork function to set up functions which acquire the lock your program uses before the fork and release it after the fork (in both the parent and child processes). Add print statements to the new functions to show what is happening.
• Submit your source code for A1part3.c on the Canvas Assignment 1 Code page.
• Answer question 7 on Canvas .
SoftEng 370 & CompSci 340 Assignment 1 - 2010
Part 4
• Find out about async-signal safety.
• Answers questions 8 and 9 on Canvas .
Resources and extra info
man pages to read:
fork, getpid, getppid
pthreads, pthread_create, pthread_self,pthread_exit, pthread_join
perror, sleep
uname (in man section 2)
ps, kill, killall
gettid
Commands to use when in a man page.
space: scroll down a page
u: scroll up half a page
d: scroll down half a page
/: followed by a string to search for and then press enter
n: find the next occurrence of the string
p: find the previous occurrence of the string
q: quit
Adding packages to Ubuntu
If you are doing this assignment on your own machine (or virtual machine) you may need to add some packages to the standard install. In Ubuntu to do anything which requires administrator privileges you have to type your admin password (you would have set this when you installed Ubuntu). All GUI programs which require administrator privileges will prompt you with a dialog box. You can do the same thing with command line programs by typing sudo before the command you want to run with administrator (super user) privileges .
To get packages you can use the command line tool apt-get e.g.
sudo apt-get install manpages-posix-dev
On Ubuntu if you want to read the pthread man pages you need to install the manpages-posix- dev package. Otherwise you will need to use the web to read them .
N.B. You do not have administrator privileges on the flexit version of Ubuntu .
Submission
Zip source code for A1part2.c and A1part3.c and submit the file on the Assignment 1 Code page. Answer the questions with example output on the Assignment 1 Questions quiz .
See the course pages for University and School information on plagiarism or cheating in assignments .
2023-08-11
Fork and threads