ࡱ> sury 'bjbj .V{{% JJJJJ^^^8\d^=T Vl"iiiSSSSSSS$GWY>SJiiiiiSJJS,,,i JJS,iS,,MDP@|_$DN&S T0=TjN7Z-*7ZLP7ZJP ii,iiiiiSSE+iii=Tiiii7Ziiiiiiiii : Operating Systems Assignment 1 Department of Mathematics and Computer Science, Lehman College, the City University of New York, Spring 2012 Due by midnight February 23 (Thursday), 2012 Preparation For this assignment, you need to set up and become familiar with a virtual machine environment (Oracle Virtual Box 4.x), or a recent Linux system (Fedora 13, 14, 15, 16, Ubuntu 10, 11). There are two options: Install Virtual Box in your PC/Windows or Mac/Mac-OS X. Create a virtual machine on Virtual Box, then install Linux as a guest operating system in the virtual machine. You can create multiple virtual machines in the Virtual Box and install various Linux guest operating systems in the created virtual machines. You may consider installing MS Windows (XP-SP3 or Windows 7) as a guest OS in a Linux host. Or on any hardware system that you can control, you need to install a Linux system (Fedora, CentOS, Ubuntu, Ubuntu Server). Tasks to Be Performed Write the following programs as explained below. Show how to compile and run the programs by typescript ($script outfile). You need to show your source program by a command (e.g., $cat n prog.c). Write a document including typescript file and answers to the questions and send the document to:  HYPERLINK "mailto:gwang.jung@lehman.cuny.edu" gwang.jung@lehman.cuny.edu With Subject: OS Sp12-A1 [FirstName.LastName] [Q.1] Program 1 (fork.c) #include #include #include #include int g = 2; int main(void) { pid_t pid; int l = 3; printf("process id(%d): parent g=%d, l=%d\n", getpid(), g, l); if((pid=fork())<0){ perror("fork error"); exit(1); } else if(pid == 0) { g++; l++; printf("I am a new child and my procedd id is: %d \n", getpid()); printf("My parent process id is: %d \n", getppid()); printf("\n The child process now terminates"); } else { g = g * 100; l = l * 300; printf("I am the PARENT process and my procedd id is: %d \n", getpid()); printf("Parent process id of the Parent Process is: %d \n", getppid()); printf("\n The parent process now terminates"); } printf("\n\n ....Who Am I ?? (%d): g=%d, l=%d\n", getpid(), g, l); //statement A printf("\n\n ....Who is my parent ?? (%d): ", getppid()); //statement B return 0; } Compile and run fork.c. Answer the following questions. What are the process IDs of the parent and the child processes? Type ps from the shell prompt and identify process ID of the nonlogin(login) shell. What is the parent process ID of the fork process? What are the values of the variables g and l of the parent process and the child process of fork, respectively? Show the output printed by statements A and B. [Q.2] Program 2 (clone.c) #include #include #include #include #include #include int g=2; int sub_func(void *arg) { g = g* 100; printf("I am a new child as a thread: my process id is: %d g=%d\n", getpid(), g); printf("My parent process id is: %d \n", getppid()); // return 0; } int main(void) { int l = 3; printf("PID(%d) : Parent g=%d, l=%d\n", getpid(), g, l); int *dynamic = malloc(4096 * sizeof(int)); //statement A clone (sub_func, (void *)(dynamic + 4095), CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, NULL); //statement B sleep(1); printf("PID(%d) : Parent g=%d, l=%d\n", getpid(), g, l); return 0; } Compile and run clone.c. Show the values of g and l printed by the parent process and the child process as a thread (by the clone() system call). Explain why you get those values. Rewrite the program by changing dynamic + 4095 to dynamic (without 4095) in statement B, and compile and run the program. Show whether you can compile and run the program. If there is any runtime error, explain why you have runtime error (core dump). Declare the following array at the beginning of the main function body. int child_stack[4096]; Also replace statements A and B by the stament shown below. clone (sub_func, (void *)(child_stack + 4095), CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, NULL); Compile and run the program. [Q.3] Program 3 (fork_exec.c) #include #include #include #include int main(void) { pid_t pid; int exit_status; if((pid=fork())<0){ perror("fork error"); exit(1); } else if(pid == 0) { printf("Hi\n"); execl("./fork", "fork", "-l", (char *)0); printf("Can I print out after execl....?? \n"); } else { pid = wait(&exit_status); printf("\n\nI am the parent of fork_exec and my process id is: %d\n", getpid()); } return 0; } Compile and run the program and show what would be the execution result of the process. [Q.4] Program 4 (fork_pt.c) #include #include #include #include #include int main(void) { int pid; printf("before fork\n\n"); if((pid = fork()) < 0){ printf("fork error\n"); exit(-2); }else if (pid == 0){ printf("TGID(%d), PID(%d) : of the child process\n", getpid(), syscall(__NR_gettid)); }else{ printf("TGID(%d), PID(%d) : of the parent processt\n", getpid(), syscall(__NR_gettid)); sleep(2); } printf("after fork\n\n"); return 0; } Compile and run the program and explain the output produced by the fork_pt process. [Q.5] Program 5 (vfork_pt.c) #include #include #include #include #include int main(void) { int pid; printf("before vfork\n\n"); if((pid = vfork()) < 0){ printf("fork error\n"); exit(-2); }else if (pid == 0){ printf("TGID(%d), PID(%d) : of the child process\n", getpid(), syscall(__NR_gettid)); _exit(0); // exit(0); }else{ printf("TGID(%d), PID(%d) : of the parent process\n", getpid(), syscall(__NR_gettid)); } printf("after vfork\n\n"); return 0; } Compile and run the program and explain the output produced. Explain why we use _exit(0) instead of exit(0) in vfork.c [Q.6] Program 6 (pthread_pt.c) #include #include #include #include #include void * t_function(void *data) { int id; int i=0; pthread_t t_id; id = *((int *)data); printf("TGID(%d), PID(%d), pthread_self(%d) : Child\n", getpid(), syscall(__NR_gettid), pthread_self()); sleep(2); return (void *)(id*id); } int main(void) { int pid, status; int a = 1; int b = 2; pthread_t p_thread[2]; printf("before pthread_create\n\n"); if((pid = pthread_create(&p_thread[0], NULL, t_function, (void*)&a)) < 0){ perror("thread create error : "); exit(1); } if((pid = pthread_create(&p_thread[1], NULL, t_function, (void*)&b)) < 0){ perror("thread create error : "); exit(2); } pthread_join(p_thread[0], (void **)&status); printf("pthread_join(%d)\n", status); pthread_join(p_thread[1], (void **)&status); printf("pthread_join(%d)\n", status); printf("TGID(%d), PID(%d) : Parent\n", getpid(), syscall(__NR_gettid)); return 0; } Compile and run the program (make sure you compile the program with lpthread option) Explain why you have the output produced by the original process (including number of threads you created). [Q.7] Program 7 (clone_pt.c) Similar to the program 2 (clone.c) #include #include #include //#include #include #include int sub_func(void *arg) { printf("TGID(%d), PID(%d) : Child\n", getpid(), syscall(__NR_gettid)); return 0; } int main(void) { int pid; int child_a_stack[4096], child_b_stack[4096]; printf("before clone\n\n"); printf("TGID(%d), PID(%d) : Parent\n", getpid(), syscall(__NR_gettid)); clone (sub_func, (void *)(child_a_stack+4095), CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID, NULL); clone (sub_func, (void *)(child_b_stack+4095), CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, NULL); //statement A sleep(1); printf("after clone\n\n"); return 0; } Compile and run the program. Explain the output produced by the process. Remove CLONE_THREAD flag from the statement A and compile and run the program again. Then explain the output produced by the process.     PAGE  PAGE 7 18EMNOV_`cd~ȶȪznzczWKW?h#CJaJnHtHhTQ;CJaJnHtHh eCJaJnHtHhr_h6/CJaJhqG<CJaJnHtHhr_h6/CJaJnHo(tHhr_h#CJaJhqG<6CJaJhr_h 66CJaJhr_h6/6CJaJ"hr_h6/6CJaJnHo(tHhr_hT k6CJaJhfEhfEnHo(tHhfEnHtHhT kCJaJnHtHhfECJaJnHtHO * 4 b c | } gdn^gdG7> & FgdG7>gdTQ;gdlgdTQ;gdTQ;$a$gdG7>$a$gdG7>  % & , < K Q j n } ~ ᲟooZNhlCJOJQJaJ(h:QhTQ;CJOJPJQJaJnHtHhlCJOJQJaJnHtHh?CJOJQJaJnHtHhTQ;CJOJQJaJnHtH$h:QhTQ;CJOJQJaJnHtHh:QhTQ;CJOJQJaJhTQ;56>*CJaJnHtHh e56>*CJaJnHtH%hG7>hG7>56>*CJaJnHtHhG7>CJaJnHtH  & = > ? {n_P_@0h@QCJOJQJaJnHtHhTQ;CJOJQJaJnHtHhlhTQ;CJOJQJaJhlhlCJOJQJaJhl6CJOJQJaJh:QhTQ;6CJOJQJaJ'h:QhTQ;6CJOJQJaJnHtH$h:QhTQ;CJOJQJaJnHtHhlCJOJQJaJh:QhTQ;CJOJQJaJhlCJOJQJaJnHtH!hl6CJOJQJaJnHtH!hTQ;6CJOJQJaJnHtH   ) * - Z e n o Ѽѩѩќ|m^R^hG7>CJaJnHtHhfEhfECJaJnHtHhr_hTQ;CJaJnHtHhTQ;h 65CJaJnHtHhTQ;hTQ;5CJaJnHtHhTQ;5CJaJnHtH$h:QhTQ;CJOJQJaJnHtH(h:QhTQ;CJOJPJQJaJnHtHh:QhTQ;CJOJQJaJhTQ;CJOJQJaJn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aJnHtH"hYh 5>*CJaJnHtHh0(5>*CJaJnHtHhYCJaJnHtHhYCJOJQJaJnHtH$hYhYCJOJQJaJnHtH-F`l<HKLghsuv';UVeg & F gdSgdYgdSgqr>JYa&`agdG7> & F gdSgdS_agW#X#$$!$8$]$&&''O'P'Q''ƴsdXƴXHsdCJaJnHtHhG7>hG7>CJaJnHtHhG7>CJOJQJaJnHtH$hG7>hG7>CJOJQJaJnHtHhG7>5>*CJaJnHtHhS5>*CJaJnHtH"hG7>hG7>5>*CJaJnHtHh0(5>*CJaJnHtHhSCJaJnHtHhShSCJaJnHtHhSCJOJQJaJnHtH   " 3 I !!.!/!U!V!!!!! "gdG7> "D"O"R"S"""""""#I#J#U#W#X##$$8$9$\$]$t$$$$ & F gdG7>gdSgdG7>$$$$$$A%L%N%O%^%`%j%%%%&&c&&&&&&'''%'Q' & FgdG7>gdG7>Q''''''''''''''''''h]hgd$| &`#$gd$| & FgdG7>''''''''''''''''''''''''h0(0JmHnHuh$| h$|0Jjh$|0JUhIYjhIYUhG7>h,ACJaJnHtH,1h/ =!"#$% b 666666666vvvvvvvvv666666>66666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~PJ_HmH nHsH tH@`@ NormalCJ_HaJmH sH tH >@>  Heading 1$@& 5CJ\Z@Z TQ; Heading 3$<@&5CJOJPJQJ\^JaJDA`D Default Paragraph FontViV 0 Table Normal :V 44 la (k ( 0No List PZ@P 0 Plain Text CJOJPJQJ^JaJnHtHNoN 0Plain Text CharCJOJPJQJ^JaJ6 @6 $|Footer 8!G$.)@!. $| Page NumberHB@2H  Body Textd,xx OJPJQJLoAL Body Text CharCJOJPJQJaJtH jSj  Table Grid7:V0@b@ n List Paragraph ^6U`q6 G7>0 Hyperlink >*B*phH@H PY0 Balloon TextCJOJ QJ ^J aJRoR PY0Balloon Text CharCJOJ QJ ^J aJtH VoV TQ;Heading 3 Char"5CJOJPJQJ\^JaJtH T:@T TQ; List Number 2 & Fd,x OJPJQJPK![Content_Types].xmlN0EH-J@%ǎǢ|ș$زULTB l,3;rØJB+$G]7O٭V$ !)O^rC$y@/yH*񄴽)޵߻UDb`}"qۋJחX^)I`nEp)liV[]1M<OP6r=zgbIguSebORD۫qu gZo~ٺlAplxpT0+[}`jzAV2Fi@qv֬5\|ʜ̭NleXdsjcs7f W+Ն7`g ȘJj|h(KD- dXiJ؇(x$( :;˹! I_TS 1?E??ZBΪmU/?~xY'y5g&΋/ɋ>GMGeD3Vq%'#q$8K)fw9:ĵ x}rxwr:\TZaG*y8IjbRc|XŻǿI u3KGnD1NIBs RuK>V.EL+M2#'fi ~V vl{u8zH *:(W☕ ~JTe\O*tHGHY}KNP*ݾ˦TѼ9/#A7qZ$*c?qUnwN%Oi4 =3N)cbJ uV4(Tn 7_?m-ٛ{UBwznʜ"Z xJZp; {/<P;,)''KQk5qpN8KGbe Sd̛\17 pa>SR! 3K4'+rzQ TTIIvt]Kc⫲K#v5+|D~O@%\w_nN[L9KqgVhn R!y+Un;*&/HrT >>\ t=.Tġ S; Z~!P9giCڧ!# B,;X=ۻ,I2UWV9$lk=Aj;{AP79|s*Y;̠[MCۿhf]o{oY=1kyVV5E8Vk+֜\80X4D)!!?*|fv u"xA@T_q64)kڬuV7 t '%;i9s9x,ڎ-45xd8?ǘd/Y|t &LILJ`& -Gt/PK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 0_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!0C)theme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] V $$$' g D''"%* PZd-g "$Q'' !#$&'()2X  '!!8@0(  B S  ?/6zN`tz 5;LO^d!'#RX  y {     , 3 6 9 @ C D L S V h n     ! ' I O ] ` l r z   6 < e l MPQ\6ARYemy  */V\ty{   25HNwz!%,/8MS %19EJKSVYhklosy#'.1:ciX_r~#,-1<?JPeq  !)06?M[^aoqy "(T`ai(.29<E!(,6SZkr~ &*14=ORadehkno| ksHK^ev{!($RY 6 9 @ C h o     ! ( ] `     H N MP *0V]  .1HObgmr>DMTioVYhkszAFPUZ_cj46JQ #*07WZ")FK\aKQ7BHORadkndi333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333c} H T iuesKW7BL&,;<jn&>? 7O^k3DIci %+jpag!F`@f=h  Pb7$9S+t$sl,dnC YhX.e]bho.n`t 0Ub04{dREt_ry&dRmzth^`.h8^8`o(() ^`hH.  L^ `LhH.  ^ `hH. x^x`hH. HL^H`LhH. ^`hH. ^`hH. L^`LhH.h^`OJQJo(hHh^`OJ QJ ^J o(hHohp^p`OJ QJ o(hHh@ ^@ `OJQJo(hHh^`OJ QJ ^J o(hHoh^`OJ QJ o(hHh^`OJQJo(hHh^`OJ QJ ^J o(hHohP^P`OJ QJ o(hH^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.h^`o(()h ^`hH.h pL^p`LhH.h @ ^@ `hH.h ^`hH.h L^`LhH.h ^`hH.h ^`hH.h PL^P`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.hh^h`o(()^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.h ^`o(hH()h ^`hH.h pLp^p`LhH.h @ @ ^@ `hH.h ^`hH.h L^`LhH.h ^`hH.h ^`hH.h PLP^P`LhH.h8^8`o(() ^`hH.  L^ `LhH.  ^ `hH. x^x`hH. HL^H`LhH. ^`hH. ^`hH. L^`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o(() ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.nCb7$9S+{de]F`REt0UbYhXmzyo.n`sl, =                  r        ,        >Z$        dY                          ̴        p                          2&        tS          YHH I * e_ %OV*E1*6;/5WNC"k"$ ~%&g'0(:)+ W.J/030|W3bs3@4C5d74 9F9}9;TQ;qG<G7>@C&sCLDjEfE I/@IB>LRXO9PmR(SsMVe^V:X9YIYPYCf[r_k=_}`W/`9a'bxpfr'gT k'k1l&~n^q6tutE|t?^wTKzS{$| 5|e*~G~:(, TlOu"!i"fDGk~=Yl;?'3,!}xwc"Vn ig 6(s >U]14P eS!m9}t r."T:b}Vucw #Rc~l$C&_2Gn6/S0,A#bg~*6#- d2K@Qy.8\ev"l g}}y*l*ZATsb,!AUt`m@!!!!@Unknown G*Ax Times New Roman5Symbol3. *Cx Arial3> | ѹ@ CNComic Sans MS;|i0Batang7@Cambria9=  @ Consolas9Garamond5. .[`)Tahoma?= *Cx Courier New;WingdingsA$BCambria Math"qhhFBg+&dh99!24 3QHP $P2! xx GJunggjungL           Oh+'0h   $ 0 <HPX`GJungNormalgjung100Microsoft Office Word@p V@J/O@a \@T|՜.+,D՜.+,L hp  CUNY9  Title 8@ _PID_HLINKSA|Mf"mailto:gwang.jung@lehman.cuny.edu  !"#$%&'()*+-./012356789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`acdefghiklmnopqtRoot Entry F)|vData ,1Table4ZWordDocument.VSummaryInformation(bDocumentSummaryInformation8jCompObjr  F Microsoft Word 97-2003 Document MSWordDocWord.Document.89q