Source Code in C
1:#include <stdio.h>
2: #include <sys/types.h>
3: #include <sys/resource.h> 4:
5: 6:
7: int pidarray[10]; 8:
9: void createfiles(int ProcessNumber,int ProcessID); 10: void parent(pid_t pid,int ProcessNumber,int status);
11: int error(char *er_message); 12: char *FileName(int ProcessNumber);
13: 14: main()
15: { 16: int ProNumb=1,status;
17: pid_t pid; 18:
19: 20: /*Process no1*/
21: pidarray[1]=getpid(); 22: createfiles(ProNumb,getpid());
23: /*Creating Process no2*/ 24: switch(pid=fork())
25: { 26: case -1:
27: error("fork"); 28: case 0:
29: { 30: /*Process no2*/
31: ProNumb=2*ProNumb; 32: pidarray[ProNumb]=getpid();
33: createfiles(ProNumb,getpid()); 34: /*Creating process no4*/
35: switch(pid=fork()) 36: {
37: case -1: 38: error("fork");
39: case 0: 40: /*Process no4*/
41: ProNumb=2*ProNumb; 42: pidarray[ProNumb]=getpid();
43: createfiles(ProNumb,getpid()); 44: pidarray[ProNumb]=0;
45: exit(ProNumb); 46: break;
47: default: 48: /*Back to Process no2*/
49: wait(&status); 50: parent(pid,ProNumb,status);
51: /*Creating Process no5*/ 52: switch(pid=fork())
53: { 54: case -1:
55: error("fork"); 56: case 0:
57: /*Process no5*/ 58: ProNumb=2*ProNumb+1;
59: pidarray[ProNumb]=getpid(); 60: createfiles(ProNumb,getpid());
61: pidarray[ProNumb]=0; 62: execl("/bin/ls","ls",(char *)0);
63: error("exec"); 64: break;
65: default: 66: /*Back to Process no2*/
67: wait(&status); 68: parent(pid,ProNumb,status);
69: /*Creating Process no6*/ 70: switch(pid=fork())
71: { 72: case -1:
73: error("fork"); 74: case 0:
75: /*Process no6*/ 76: ProNumb=2*ProNumb+2;
77: pidarray[ProNumb]=getpid(); 78: createfiles(ProNumb,getpid());
79: pidarray[ProNumb]=0; 80: execl("/bin/date","date",(char *)0);
81: error("exec"); 82: break;
83: default: 84: /*Back to Process no2*/
85: wait(&status); 86: parent(pid,ProNumb,status);
87: /*Process no2*/ 88: pidarray[ProNumb]=0;
89: exit(ProNumb); 90: }
91: } 92: }
93: break; 94: }
95: default: 96: /*Back to root process*/
97: wait(&status); 98: parent(pid,ProNumb,status);
99: /*Creating process no3*/ 100: switch(pid=fork())
101: { 102: case -1:
103: error("fork"); 104: case 0:
105: /*Process no3*/ 106: ProNumb=2*ProNumb+1;
107: pidarray[ProNumb]=getpid(); 108: createfiles(ProNumb,getpid());
109: /*Creating process no7*/ 110: switch(pid=fork())
111: { 112: case -1:
113: error("fork"); 114: case 0:
115: /*Process no7*/ 116: ProNumb=2*ProNumb+1;
117: pidarray[ProNumb]=getpid(); 118: createfiles(ProNumb,getpid());
119: pidarray[ProNumb]=0; 120: exit(ProNumb);
121: break; 122: default:
123: /*Back to Process no3*/ 124: wait(&status);
125: parent(pid,ProNumb,status); 126: /*Creating process no8*/
127: switch(pid=fork()) 128: {
129: case -1: 130: error("fork");
131: case 0: 132: /*Process no8*/
133: ProNumb=2*ProNumb+2; 134: pidarray[ProNumb]=getpid();
135: createfiles(ProNumb,getpid()); 136: pidarray[ProNumb]=0;
137: execl("/bin/ls","ls","-a","-s",(char *)0); 138: error("exec");
139: break; 140: default:
141: /*Back to Process no3*/ 142: wait(&status);
143: parent(pid,ProNumb,status); 144: /*Creating process no9*/
145: switch(pid=fork()) 146: {
147: case -1: 148: error("fork");
149: case 0: 150: /*Process no9*/
151: ProNumb=2*ProNumb+3; 152: pidarray[ProNumb]=getpid();
153: createfiles(ProNumb,getpid()); 154: pidarray[ProNumb]=0;
155: execl("/bin/du","du","-a",(char *)0); 156: //error("exec");
157: break; 158: default:
159: /*Back to process no3*/ 160: wait(&status);
161: parent(pid,ProNumb,status); 162: pidarray[ProNumb]=0;
163: exit(ProNumb); 164: }
165: } 166: }
167: break; 168: default:
169: /*Back to Process no1*/ 170: wait(&status);
171: parent(pid,ProNumb,status); 172: pidarray[ProNumb]=0;
173: exit(ProNumb); 174: }
175: } 176:
177: } 178:
179: 180: void createfiles(int ProcessNumber,int ProcessID)
181: { 182:
183: int i; 184: FILE *Fileptr;
185: 186: Fileptr=fopen(FileName(ProcessNumber),"w");
187: fprintf(Fileptr,"Process Number is %d\n",ProcessNumber); 188: fprintf(Fileptr,"Process ID is %d\n",ProcessID);
189: fprintf(Fileptr,"Process Number PID\n"); 190: for(i=1;i<ProcessNumber+1;i++)
191: { 192: if(pidarray[i]!=0)
193: fprintf(Fileptr,"%2d %20d\n",i,pidarray[i]); 194: }
195: 196: sleep(ProcessNumber);
197: fclose(Fileptr); 198:
199: 200: }
201: 202: void parent(pid_t pid,int ProcessNumber,int status)
203: { 204: struct rusage r_usage;
205: FILE *Fileptr; 206:
207: Fileptr=fopen(FileName(ProcessNumber),"a"); 208: fprintf(Fileptr,"My child process with PID %5d has exited with code %2d\n",pid,status>>8);
209: getrusage(RUSAGE_CHILDREN, &r_usage); 210: fprintf(Fileptr,"Usertime of my child is %06ld and systemtime is %06ld\n",r_usage.ru_utime.tv_usec, r_usage.ru_stime.tv_usec);
211: fclose(Fileptr); 212:
213: 214: }
215: 216:
217: int error(char *er_message) 218: {
219: perror(er_message); 220: exit(1);
221: } 222:
223: 224: char *FileName(int ProcessNumber)
225: { 226: char *name;
227: 228: switch(ProcessNumber)
229: { 230: case 1:
231: name="File1"; 232: break;
233: case 2: 234: name="File2";
235: break; 236: case 3:
237: name="File3"; 238: break;
239: case 4: 240: name="File4";
241: break; 242: case 5:
243: name="File5"; 244: break;
245: case 6: 246: name="File6";
247: break; 248: case 7:
249: name="File7"; 250: break;
251: case 8: 252: name="File8";
253: break; 254: case 9:
255: name="File9"; 256: break;
257: default: 258: exit(1);
259: 260: }
261: 262: return name;
263: 264: }