C/C++ Programming in a UNIX Environment, CS 3377, Assignment 06 1. [25] Write a program that prints “Hello, World!” from a child process it creates and prints the ids of the child and parent processes...

1 answer below »
This Assignment must be done in C and should be able to execute on Linux environment. On Linux Platforms such as Mobaxterm. Follow Instruction in the file. and I will provide some relative information so that it is easier for the expert. Please Donot reference any online sources like chegg. Due to plagiarism concerns.


C/C++ Programming in a UNIX Environment, CS 3377, Assignment 06 1. [25] Write a program that prints “Hello, World!” from a child process it creates and prints the ids of the child and parent processes in the format “The child and parent process ids are: 1234 and 1235.” A failure of fork() should be caught and printed as an error message using err_sys call. 2. [50] The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, …. It is defined by the following mathematical expression, with x0 and x1 being 0 and 1, respectively: xn = xn-1 + xn-2 Write a C program using the fork() system call that generates and prints the Fibonacci sequence in the child process. The number of members in the sequence will be determined by a user provided as a user prompted input. Make the parent process wait for the child process to complete before exiting. Make the program print both the parent and child process IDs. The program should perform the necessary error checking to only accept positive integers from the user. A failure of fork() should be caught and printed as an error message using err_sys call. 3. [25] Write a program using the Pthreads API that prints “Hello, World!” from four threads it creates and prints the ids of the threads along with in the format “Hello, World! from thread id : 0” from each of the threads. You can use a global variable to declare the number of threads to be created and set that to 4 for this assignment. Reading for Week 11 and 12  Chapter 8 of Advacned Programming in the UNIX Environment Stevens and Rago. (Week 11) Process Control  Chapter 11 of Advacned Programming in the UNIX Environment Stevens and Rago. (Week 12) Threads 2 Agenda  UNIX Process Creation and Management  Threads: Pthreads 3 4 CPU Process Switching by OS Silberschatz, Galvin, and Gagne©1999 5 Process-based Operating System  Most of OS executes within user processes  Uses two categories of processes ◼ System processes ◼ User processes 6 Unix Process State Transition Diagram Sleep = Blocked 7 UNIX Process Creation  Every process, except process 0, is created by the fork() system call ◼ fork() allocates entry in process table and assigns a unique PID to the child process ◼ child gets a copy of process image of parent: both child and parent share the same code following fork(), different data ◼ but fork() returns the PID of the child to the parent process and returns 0 to the child process ◼ Optional Exec() system call can be used after a fork to replace the process’ memory space with a new program 8 UNIX Process Creation  Parent process can wait() for completion of child ◼ The child process can pass data back to parent via exit() call, picked up by parent via the wait(). ◼ Terminated child is a “zombie” if parent does not wait() for it 9 UNIX System Processes  “Boot” loads the kernel image  Process 0 is created at boot time and becomes the “swapper” after forking process 1 (the INIT process)  When a user logs in: process 1 creates a process for that user 10 Unix Tree of Processes Silberschatz, Galvin, and Gagne©1999 11 Unix Subprocesses: System calls  #include  pid_t fork() ◼ Creates new process image which is an (almost) exact copy of the one that invokes it  int execv(char*filename,char* argv[])  int execl(char*filename,char* arg0, char* arg1,… NULL) ◼ Replace current process image with one running the named program 12 Wait functions  #include  pid_t waitpid(pid_t pid, int* status_ptr, int options) ◼ Waits for completion of a particular child process  pid_t wait(int* status_ptr) ◼ Waits for any one child to terminate  pid_t getpid(void) ◼ Returns process ID  pid_t getppid(void) (parent ID) Processes on typical Solaris Process Creation  Assign a unique process identifier (PID).  Allocate space for the process image.  Initialize process control block ◼ many default values (e.g., state is New, no I/O devices or files...).  Set up appropriate linkages ◼ Ex: add new process to linked list used for the scheduling queue. Process Creation  Address space ◼ Child duplicate of parent. ◼ Child has a program loaded into it.  UNIX examples ◼ fork system call creates new process. ◼ exec system call used after a fork to replace the process’ memory space with a new program. Process Creation C Program Forking int main() { pid_t pid; /* fork another process */ pid = fork(); //there are two processes now if (pid < 0) { /* error occurred */ fprintf(stderr, "fork failed"); exit(-1); } else if (pid == 0) { /* child process , child process executes here*/ execlp("/bin/ls", "ls", null); } else { /* parent process, parent process executes here */ /* parent will wait for the child to complete */ wait (null); /* null returned by the child process in the end */ printf ("child complete"); exit(0); } } shared memory architecture copyright © 2010, elsevier inc. all rights reserved processes and threads  a process is an instance of a running (or suspended) program.  threads are analogous to a “light- weight” process.  in a shared memory program a single process may have multiple threads of control. copyright © 2010, elsevier inc. all rights reserved logical view of threads  threads are created within a process p1 shsh sh foo t1 process hierarchya process t2 t4 t5 t3 shared code, data and kernel context concurrent thread execution  two threads run concurrently if their logical flows overlap in time  otherwise, they are sequential (we’ll see that processes have a similar rule)  examples: ◼ concurrent: a & b, a&c ◼ sequential: b & c time thread a thread b thread c threads  “a thread is a basic unit of cpu utilization;” ◼ cpu is dispatched to execute threads. ◼ threads are scheduled for cpu cycles. ◼ thread within the same process share a code section, data section, and other resources such as open files and signals. (signals discussed later).  idea behind threads is that the process can perform many different activities at the same time (concurrently, of course, on a single cpu) 22 threads  processes with a single thread of control can perform one task at a time  management of multiple threads of control essential now for concurrent execution of tasks  most modern desktop and notebook computers have multiple cores  what is multiple threads of control? → 23 multiple threads of control  most modern applications are developed as a separate process with several threads of control. ◼ number of separate ‘tasks’ that are part of the application can be executed concurrent with other tasks. ◼ example: word processor  echoing keystrokes  undertaking styling formatting  checking and doing automatic correction ◼ if these tasks had to be executed sequentially, performance would be unbearable or simply would not be done thus reducing the overall quality of, say, a document. multiple threads of control  process creation: ◼ process creation is time consuming ◼ way of doing business before we used threads.  multiple threads for doing disjoint tasks concurrently  solution is to have the server create a single thread that ‘listens’ for new requests for service.  when received, server creates a thread to service the request.  examples: ◼ rpcs are multi-threaded ◼ kernels are multi-threaded single and multithreaded processes it is important to note, however, that a multi-threaded process shares resources with its siblings and parent process. multithreading benefits  responsiveness  resource sharing  economy – since threads share resources, it is easier to context-switch threads than context- switching processes.  utilization of mp architectures – significant increases in performance in a multiprocessor system, where different threads may be running simultaneously (in parallel) on multiple processors.  of course, there’s never ‘a free lunch,’ as we will see later. (there’s always a cost…; nothing this good comes free. ☺ ) multicore programming 28 challenges for multicore programming: • dividing activities • balance • data splitting • data dependency • test and debug multithreading  performing multiple threads of execution in parallel ◼ replicate registers, pc, etc. ◼ fast switching between threads  fine-grain multithreading ◼ switch threads after each cycle ◼ interleave instruction execution ◼ if one thread stalls, others are executed  coarse-grain multithreading ◼ only switch on long stall (e.g., l2-cache miss) ◼ simplifies hardware, but doesn’t hide short stalls (eg, data hazards) 29 simultaneous multithreading  in multiple-issue dynamically scheduled processor ◼ schedule instructions from multiple threads ◼ instructions from independent threads execute when function units are available ◼ within threads, dependencies handled by scheduling and register renaming  example: intel pentium-4 ht ◼ two threads: duplicated registers, shared function units and caches 30 multithreading example 31 32 shared memory programming several thread libraries/systems  pthreads is the posix standard ◼ relatively low level ◼ portable but possibly slow; relatively heavyweight  openmp standard for application level programming ◼ support for scientific programming on shared memory ◼ http://www.openmp.org  java threads  tbb: thread building blocks ◼ intel  cilk: language of the c “ilk” ◼ lightweight threads embedded into c 33 overview of posix threads  posix: 0)="" {="" *="" error="" occurred="" */="" fprintf(stderr,="" "fork="" failed");="" exit(-1);="" }="" else="" if="" (pid="=" 0)="" {="" *="" child="" process="" ,="" child="" process="" executes="" here*/="" execlp("/bin/ls",="" "ls",="" null);="" }="" else="" {="" *="" parent="" process,="" parent="" process="" executes="" here="" */="" *="" parent="" will="" wait="" for="" the="" child="" to="" complete="" */="" wait="" (null);="" *="" null="" returned="" by="" the="" child="" process="" in="" the="" end="" */="" printf="" ("child="" complete");="" exit(0);="" }="" }="" shared="" memory="" architecture="" copyright="" ©="" 2010,="" elsevier="" inc.="" all="" rights="" reserved="" processes="" and="" threads="" ="" a="" process="" is="" an="" instance="" of="" a="" running="" (or="" suspended)="" program.="" ="" threads="" are="" analogous="" to="" a="" “light-="" weight”="" process.="" ="" in="" a="" shared="" memory="" program="" a="" single="" process="" may="" have="" multiple="" threads="" of="" control.="" copyright="" ©="" 2010,="" elsevier="" inc.="" all="" rights="" reserved="" logical="" view="" of="" threads="" ="" threads="" are="" created="" within="" a="" process="" p1="" shsh="" sh="" foo="" t1="" process="" hierarchya="" process="" t2="" t4="" t5="" t3="" shared="" code,="" data="" and="" kernel="" context="" concurrent="" thread="" execution="" ="" two="" threads="" run="" concurrently="" if="" their="" logical="" flows="" overlap="" in="" time="" ="" otherwise,="" they="" are="" sequential="" (we’ll="" see="" that="" processes="" have="" a="" similar="" rule)="" ="" examples:="" ◼="" concurrent:="" a="" &="" b,="" a&c="" ◼="" sequential:="" b="" &="" c="" time="" thread="" a="" thread="" b="" thread="" c="" threads="" ="" “a="" thread="" is="" a="" basic="" unit="" of="" cpu="" utilization;”="" ◼="" cpu="" is="" dispatched="" to="" execute="" threads.="" ◼="" threads="" are="" scheduled="" for="" cpu="" cycles.="" ◼="" thread="" within="" the="" same="" process="" share="" a="" code="" section,="" data="" section,="" and="" other="" resources="" such="" as="" open="" files="" and="" signals.="" (signals="" discussed="" later).="" ="" idea="" behind="" threads="" is="" that="" the="" process="" can="" perform="" many="" different="" activities="" at="" the="" same="" time="" (concurrently,="" of="" course,="" on="" a="" single="" cpu)="" 22="" threads="" ="" processes="" with="" a="" single="" thread="" of="" control="" can="" perform="" one="" task="" at="" a="" time="" ="" management="" of="" multiple="" threads="" of="" control="" essential="" now="" for="" concurrent="" execution="" of="" tasks="" ="" most="" modern="" desktop="" and="" notebook="" computers="" have="" multiple="" cores="" ="" what="" is="" multiple="" threads="" of="" control?="" →="" 23="" multiple="" threads="" of="" control="" ="" most="" modern="" applications="" are="" developed="" as="" a="" separate="" process="" with="" several="" threads="" of="" control.="" ◼="" number="" of="" separate="" ‘tasks’="" that="" are="" part="" of="" the="" application="" can="" be="" executed="" concurrent="" with="" other="" tasks.="" ◼="" example:="" word="" processor="" ="" echoing="" keystrokes="" ="" undertaking="" styling="" formatting="" ="" checking="" and="" doing="" automatic="" correction="" ◼="" if="" these="" tasks="" had="" to="" be="" executed="" sequentially,="" performance="" would="" be="" unbearable="" or="" simply="" would="" not="" be="" done="" thus="" reducing="" the="" overall="" quality="" of,="" say,="" a="" document.="" multiple="" threads="" of="" control="" ="" process="" creation:="" ◼="" process="" creation="" is="" time="" consuming="" ◼="" way="" of="" doing="" business="" before="" we="" used="" threads.="" ="" multiple="" threads="" for="" doing="" disjoint="" tasks="" concurrently="" ="" solution="" is="" to="" have="" the="" server="" create="" a="" single="" thread="" that="" ‘listens’="" for="" new="" requests="" for="" service.="" ="" when="" received,="" server="" creates="" a="" thread="" to="" service="" the="" request.="" ="" examples:="" ◼="" rpcs="" are="" multi-threaded="" ◼="" kernels="" are="" multi-threaded="" single="" and="" multithreaded="" processes="" it="" is="" important="" to="" note,="" however,="" that="" a="" multi-threaded="" process="" shares="" resources="" with="" its="" siblings="" and="" parent="" process.="" multithreading="" benefits="" ="" responsiveness="" ="" resource="" sharing="" ="" economy="" –="" since="" threads="" share="" resources,="" it="" is="" easier="" to="" context-switch="" threads="" than="" context-="" switching="" processes.="" ="" utilization="" of="" mp="" architectures="" –="" significant="" increases="" in="" performance="" in="" a="" multiprocessor="" system,="" where="" different="" threads="" may="" be="" running="" simultaneously="" (in="" parallel)="" on="" multiple="" processors.="" ="" of="" course,="" there’s="" never="" ‘a="" free="" lunch,’="" as="" we="" will="" see="" later.="" (there’s="" always="" a="" cost…;="" nothing="" this="" good="" comes="" free.="" ☺="" )="" multicore="" programming="" 28="" challenges="" for="" multicore="" programming:="" •="" dividing="" activities="" •="" balance="" •="" data="" splitting="" •="" data="" dependency="" •="" test="" and="" debug="" multithreading="" ="" performing="" multiple="" threads="" of="" execution="" in="" parallel="" ◼="" replicate="" registers,="" pc,="" etc.="" ◼="" fast="" switching="" between="" threads="" ="" fine-grain="" multithreading="" ◼="" switch="" threads="" after="" each="" cycle="" ◼="" interleave="" instruction="" execution="" ◼="" if="" one="" thread="" stalls,="" others="" are="" executed="" ="" coarse-grain="" multithreading="" ◼="" only="" switch="" on="" long="" stall="" (e.g.,="" l2-cache="" miss)="" ◼="" simplifies="" hardware,="" but="" doesn’t="" hide="" short="" stalls="" (eg,="" data="" hazards)="" 29="" simultaneous="" multithreading="" ="" in="" multiple-issue="" dynamically="" scheduled="" processor="" ◼="" schedule="" instructions="" from="" multiple="" threads="" ◼="" instructions="" from="" independent="" threads="" execute="" when="" function="" units="" are="" available="" ◼="" within="" threads,="" dependencies="" handled="" by="" scheduling="" and="" register="" renaming="" ="" example:="" intel="" pentium-4="" ht="" ◼="" two="" threads:="" duplicated="" registers,="" shared="" function="" units="" and="" caches="" 30="" multithreading="" example="" 31="" 32="" shared="" memory="" programming="" several="" thread="" libraries/systems="" ="" pthreads="" is="" the="" posix="" standard="" ◼="" relatively="" low="" level="" ◼="" portable="" but="" possibly="" slow;="" relatively="" heavyweight="" ="" openmp="" standard="" for="" application="" level="" programming="" ◼="" support="" for="" scientific="" programming="" on="" shared="" memory="" ◼="" http://www.openmp.org="" ="" java="" threads="" ="" tbb:="" thread="" building="" blocks="" ◼="" intel="" ="" cilk:="" language="" of="" the="" c="" “ilk”="" ◼="" lightweight="" threads="" embedded="" into="" c="" 33="" overview="" of="" posix="" threads="" ="">
Answered Same DayApr 06, 2021

Answer To: C/C++ Programming in a UNIX Environment, CS 3377, Assignment 06 1. [25] Write a program that prints...

Vibhav answered on Apr 07 2021
147 Votes
threads/err.h
#include /* for definition of errno */
#include /* ISO C variable aruments */
    #include
    #in
clude
    
    #define MAXLINE 512
static void err_doit(int, int, const char *, va_list);
/*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}
/*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
err_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
*/
void
err_exit(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
void
err_dump(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort();/* dump core and terminate...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here