return 1;
}
printf("Pid: %ld process locked the file.\n",(long)getpid());
sleep(1);
printf("Pid: %ld process release the file.\n",(long)getpid());
return 0;
}
【执行结果】
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ gcc -o filelock systemcall2.c
gaolu@gaolu-desktop:~$./filelock testlock.dat& ./filelock testlock.dat& ./filelock testlock.dat &
Pid: 6488 process locked the file.
[1] 6488
[2] 6489
[3] 6490
gaolu@gaolu-desktop:~$ Pid: 6488 process release the file.
Pid: 6490 process locked the file. //阻塞到进程6488释放锁的时候申请到
Pid: 6490 process release the file.
Pid: 6489 process locked the file.
Pid: 6489 process release the file. //阻塞到进程6490释放锁的时候申请到锁
[1] Done ./filelock testlock.dat
[2]- Done ./filelock testlock.dat
[3]+ Done ./filelock testlock.dat
gaolu@gaolu-desktop:~$