-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4_question3.c
65 lines (57 loc) · 1.21 KB
/
lab4_question3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/time.h> //gettimeofday
#include <time.h> //time
#include <sys/stat.h>
int main(){
int myflag;
char buf[256];
int status=0;
char myinput1[256];
char myinput2[256];
printf("enter first command\n");
scanf("%s",myinput1);
printf("enter second command that you want to replace\n");
scanf("%s",myinput2);
struct timeval start,stop;
srand(time(NULL));
gettimeofday(&start,NULL);
sleep(rand()%10);
pid_t pid;
pid=fork();
int inChild=0;
if(pid==0)
{
execl(".",myinput2,myinput1);
inChild=1;
}
if (pid!=0)
{
myflag=wait(&status);
if(myflag>0){
gettimeofday(&stop,NULL);
long sec=stop.tv_sec-start.tv_sec;
float m1=start.tv_usec;
float m2=stop.tv_usec;
long elapsed = sec*1000+(m2-m1)/1000;
printf("%ld\n",elapsed);
sprintf(buf,"%ld",elapsed);
int openFile=open("n.txt",O_CREAT | O_TRUNC| O_RDWR,00777);
write(openFile,buf,strlen(buf));
}
}
/*while(inChild==0)
{
printf("this is parent\n");
sleep(1);
}
while(inChild==1)
{
printf("this is Child\n");
sleep(1);
}*/
}