Lab#4 (CS500 OSD, Spring, 2016)

Due Date/Time:

Before class, Tuesday June 21, 2016 (Tuesday session)

Before class, ThursdayJune 23, 2016 (Thursday session)

Before class, Friday June 24, 2016 (Friday session)

Before class, Saturday June 25, 2016 (Saturday session)

Before class, Sunday June 26, 2016 (Sunday session)

Note: You cannot earn this extra credit without finishing your individual case study. Submission after the deadline gets only partial credit.

  • Turn in your lab results with screen shots to your graders, including the answers to the question in red.
  • Each group assigned by graders just need to submit a report.

Group number: ______

Student ID:______Name: ______

Student ID:______Name: ______

Student ID:______Name: ______

Student ID:______Name: ______

1

Summer 2015Silicon Valley University

Lab Description

A thread is the core element of a multi-tasking programming environment. The thread is created from a process (i.e. parent process). Parent process shares its Process Identifier (PID) and Process Control Block (i.e. task_struct in Linux) with child threads. Threads are created either by a user process in user space (user-level threading) or by a kernel process in kernel space (kernel-level threading). Since the thread has its own thread control block, the thread can run concurrently with other threads created by the parent process. This allows the process to exploit parallelism in a multi-processor computer system. In the lab, you will run a user program and create multiple threads and display the process information using the “ps” (Process Status) command.

Lab Procedure

1)Use vi editor to enter the C program below to file proc_thread.c.

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int X; // global (shared) variable

void *foo(void *data)

{

int n = atoi( data );

for( int i = 0; i < n; i++) {

printf("Thread foo (%d)\n", X++);

sleep(10); /* Sleep for 10 seconds. */

}

return NULL;

}

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

pthread_t tid;

pthread_t tid2;

pthread_t tid3;

pthread_t tid4;

pthread_attr_t attr;

printf("::main:: argc %d\n", argc);

if (argc != 2) {

printf("usage: <integer value>\n"); return -1;

}

pthread_attr_init(&attr);

pthread_create(&tid, &attr, foo, argv[1]);

pthread_create(&tid2, &attr, foo, argv[1]);

pthread_create(&tid3, &attr, foo, argv[1]);

pthread_create(&tid4, &attr, foo, argv[1]);

pthread_join(tid, NULL);

pthread_join(tid2, NULL);

pthread_join(tid3, NULL);

pthread_join(tid4, NULL);

return 0;

}

2)Compile the C program:

gcc proc_thread.c -o proc_thread -lpthread

NOTE: When recompiling proc_thread.c, make sure to remove the object file, proc_thread.

rm proc_thread

3)Execute the image.

(whoami); ./proc_thread 4

Q#1: Take a snapshot of your screen to show your works, including your user name.

Answer (1pt): ______

The output should be similar to:

::main:: argc 2

Thread foo (0)

Thread foo (1)

Thread foo (2)

Thread foo (3)

Thread foo (4)

Thread foo (5)

Thread foo (6)

Thread foo (6)

Thread foo (7)

Thread foo (8)

Thread foo (9)

Thread foo (10)

Thread foo (11)

Thread foo (11)

Thread foo (12)

Thread foo (12)

Q#2: Why are there some of numbers repeated, like 6,11, or 12, as shown in the above output?Answer (1pt): ______

4)While the program is running, show the process status using the ps command.

ps -lL NOTE: The syntax is lower case “l” and upper case “L”.

Q#3: Show the output. Take a snapshot of your screen to show your works, including your user name. Answer (1pt): ______

The output should be similar to:

cs500in

ps -lL

F S UID PID PPID LWP C PRI NI ADDR SZ WCHAN TTY TIME CMD

0 S 500 2018 2016 2018 0 80 0 - 26959 wait pts/0 00:00:00 bash

0 S 500 4177 2018 4177 0 80 0 - 11772 futex_ pts/0 00:00:00 proc_thread

1 S 500 4177 2018 4178 0 80 0 - 11772 hrtime pts/0 00:00:00 proc_thread

1 S 500 4177 2018 4179 0 80 0 - 11772 hrtime pts/0 00:00:00 proc_thread

1 S 500 4177 2018 4180 0 80 0 - 11772 hrtime pts/0 00:00:00 proc_thread

1 S 500 4177 2018 4181 0 80 0 - 11772 hrtime pts/0 00:00:00 proc_thread

0 R 500 4182 2018 4182 1 80 0 - 26368 - pts/0 00:00:00 ps

5)While the program is running, show the process status using the ps command.

ps -l NOTE: The syntax is lower case “l”.

Q#4: Take a snapshot of your screen to show your works, including your user name. Answer (1pt): ______

The output should be similar to:

cs500in

ps -l

F S UID PID PPID LWP C PRI NI ADDR SZ WCHAN TTY TIME CMD

0 S 500 2018 2016 2018 0 80 0 - 26959 wait pts/0 00:00:00 bash

0 S 500 4177 2018 4177 0 80 0 - 11772 futex_ pts/0 00:00:00 proc_thread

0 R 500 4182 2018 4182 1 80 0 - 26368 - pts/0 00:00:00 ps

6)The following is the explanation of the difference between the first output (“ps -lL”) and the second output (“ps -l”).

The first output shows the Light-Weight Processes (LWP) that was created when the user-level threads were created. The second output shows only the processes.

Q#5: What are the PID’s of the parent and child process, respectively? Are they the same? Answer (1pt): ______

Your experimental results should show the same PID for parent process and all its child threads. Share since child threads share the same resources of the parent process,

7)The heading LWP represents “Light-Weight Process”. The following is the meaning of a Light-Weight Process.

A Light-Weight Process (LWP) is a kernel thread created by the kernel to support the user-space thread. The LWP is “light-weight” since it shares the resource of the parent process (the address space, virtual memory pages (VM), signal handlers, file descriptors ) and does not require as much resources as a regular process ( i.e. “heavy-weight” process ).

8)Compare the LWP and the PID of the child threads and of the parent process or other processes. The differences is explained as follows:

When the user-level thread is created, a kernel-level thread is also created. The PThread library implements a one-to-one threading model. The kernel-level thread is a LWP and scheduled by the kernel based on its PID. Therefore, the LWP column shows a one-to-one mapping between the user-level thread and the kernel-level thread.

Summer 2015Silicon Valley University