2
CSCE 313: Introduction to Computer Systems (Fall 2013) HW #1: 25 points (due on Sep. 24, at 11:10 am - CSNET) Please submit a zipped file containing your answers (.doc or .pdf) and your codes. 1. (a) What are two differences between user-level threads and kernel-level threads? (b) Under what circumstances is one type better than the other? (2 points) 2. (a) What resources are used when a thread is created? (b) How do they differ from those used when a process is created? (2 points) 3. How do the Linux fork and clone system calls differ? How are they alike? (2 points) 4. (a) What is the main advantage of multiprogramming and timesharing? (b) What is the difference between them? (c) What is the main advantage/disadvantage of each mechanism? (3 points) 5. Compile and execute the following code on “linux.cse.tamu.edumachine for (a) n=4, and (b) n=5. Draw the tree of processes produced by each run (i.e., run (a) and run (b)) and also show the output lines of each run. (Note: you may use other machines to compile/execute your code and also increase/decrease the sleep time and use pscommand to have a better understanding of what the code does) (6 points) #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main (int argc, char *argv[]) { pid_t childpid = 0; int i, n; if (argc != 2){ /* check for valid number of command-line arguments */ fprintf(stderr, "Usage: %s processes\n", argv[0]); return 1; } n = atoi(argv[1]); for (i = 1; i < n; i++) if ((childpid = fork()) == -1) break; sleep(2); /*optinal, but may help to see a better output*/ fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n",i, (long)getpid(), (long)getppid(), (long)childpid); sleep(2); /*you may increase it when using ps command*/ return 0; }

Hw1

Embed Size (px)

DESCRIPTION

SHPE newsletter for set

Citation preview

Page 1: Hw1

CSCE 313: Introduction to Computer Systems (Fall 2013) HW #1: 25 points (due on Sep. 24, at 11:10 am - CSNET)

Please submit a zipped file containing your answers (.doc or .pdf) and your codes. 1. (a) What are two differences between user-level threads and kernel-level threads?

(b) Under what circumstances is one type better than the other? (2 points)

2. (a) What resources are used when a thread is created? (b) How do they differ from those used when a process is created? (2 points)

3. How do the Linux fork and clone system calls differ? How are they alike? (2 points)

4. (a) What is the main advantage of multiprogramming and timesharing? (b) What is the difference between them? (c) What is the main advantage/disadvantage of each mechanism? (3 points)

5. Compile and execute the following code on “linux.cse.tamu.edu” machine for (a) n=4, and (b) n=5. Draw the tree of processes produced by each run (i.e., run (a) and run (b)) and also show the output lines of each run. (Note: you may use other machines to compile/execute your code and also increase/decrease the sleep time and use “ps” command to have a better understanding of what the code does) (6 points)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main (int argc, char *argv[]) {

pid_t childpid = 0;

int i, n;

if (argc != 2){ /* check for valid number of command-line

arguments */

fprintf(stderr, "Usage: %s processes\n", argv[0]);

return 1;

}

n = atoi(argv[1]);

for (i = 1; i < n; i++)

if ((childpid = fork()) == -1)

break;

sleep(2); /*optinal, but may help to see a better output*/

fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child

ID:%ld\n",i, (long)getpid(), (long)getppid(), (long)childpid);

sleep(2); /*you may increase it when using ps command*/

return 0;

}

Page 2: Hw1

6. (Programming Assignment) Write a C/C++ program (call it string_invert) that takes a string argument from the command line and outputs the string in reversed order.

a. Constraint 1: Each process can output at most one character. If you want to output more than a single character, you must fork off one or more processes in order to do that, and each of the forked processes in turn outputs a single character.

b. Constraint 2: Each process can fork-o_ at most one other process.

After the call to program string invert with the command line argument, the output should appear, and no more processes should be running, in addition to the shell. Test your program on any UNIX/LINUX machine, and turn in the source code as part of the written assignment. (The source code should be at most a few lines long.) (10 points)