Processes and Threads Tutorial

Questions

Processes and Threads

Q1: In the three-state process model, what do each of the three states signify? What transitions are possible between each of the states, and what causes a process (or thread) to undertake such a transition?

Q2: Compare cooperative versus preemptive multithreading?

Q3: Describe user-level threads and kernel-level threads. What are the advantages or disadvantages of each approach?

Q4: Describe a plausible sequence of activities that occur when a timer interrupt results in a context switch.

Q5: The following segment of code is similar (but much simpler) to the main task that the daemon inetd performs. It accepts connections on a socket and forks a process to handle the connection.

This is not guaranteed to be compilable. Use the man command if you want to investigate what all the system calls are doing.

0001 xxx(int socket){
0002 
0003        while ((fd = accept(socket, NULL, NULL)) >= 0) {
0004                switch((pid = fork())) {
0005                case -1:
0006                        syslog(LOG_WARN, "%s cannot create process: %s",
0007				progname, sys_error(errno));
0008                        continue;
0009                case 0:         
0010                        close(0);
0011                        close(1);
0012                        dup(fd);
0013                        dup(fd);
0014                        execl("/usr/sbin/handle_connection", 
0015				"handle_connection", NULL);
0016                        syslog(LOG_WARN, "%s cannot exec handle_connection\ 
0017				helper : %s", progname, sys_error(errno));
0018                        _exit(0);
0019                default: 
0020                        waitpid(pid, &status, 0); 
0021                        if (WIFEXITED(status) && WIFEXITSTATUS(status) == 0)
0022                                continue;
0023                        syslog(LOG_WARN, "handle_connection failed:\
0024				exit status +%d\n", status);
0025                }
0026        }
0027 }

Q6: A web server is constructed such that it is multithreaded. If the only way to read from a file is a normal blocking read system call, do you think user-level threads or kernel-level threads are being used for the web server? Why?

Q7: Compare reading a file using a single-threaded file server and a multithreaded file server. Within the file server, it takes 15 msec to get a request for work and do all the necessary processing, assuming the required block is in the main memory disk block cache. A disk operation is required for one third of the requests, which takes an additional 75 msec during which the thread sleeps. How many requests/sec can a server handled if it is single threaded? If it is multithreaded?