1. pid .
Ec_neg1 (waitpid (pid, & status, 0))
2. - , . * /
Ec_neg1 (pid = waitpid (-1, NULL, 0))
3. - pgid . , . * /
Ec_neg1 (pid = waitpid (-pgid, & status, WNOHANG | WUNTRACED))
wait waitpid pid, -1, options :
wait -
# include <sys/wait.h>
pid_t wait (int * statusp,
statusp - NULL;
-1 ( - errno).
wait , - . , , , , , ' . waitpid , , no , . ͳ wait , .
, , . , - , waitpid . - waitpid WNOHANG . , , waitpid c pid, -1, . , , (, 䳿 ) . , wait, , , , .
- , , 5 - . , x. , , .
#include <stdio.h>
#include <stdlib.h>
|
|
#include <unistd.h>
#include <errno.h>
int main()
{
int x, pid;
x=2;
printf("Single process, x=%d\n",x);
pid=fork();
if(pid == 0)
printf("New, x=%d\n",x); //
else if(pid > 0)
{ //
printf("Old, pid=%d, x=%d\n",pid,x);
sleep(5);
wait(pid);
}
else
{ perror("Fork error ");
return -1;
}
return 0;
}
, exec , ' .
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
main()
{
pid_t pid;
switch (pid = fork())
{
case -1:
printf( fork);
break;
case 0: /* exec */
execl (/bin/ls, ls, -l, (char *)0);
printf( exec);
break;
default: /* wait */
/* . */
wait ((int *)0);
printf ( ls \n);
exit (0);
}
}
7.
1. 1 . system() ps, 1. . ps 2. .
2. 1 . exec(), ps. ps . ps .
3. var , 1. , . 1 var, , , . 3, 5. . .
4. 1 . system() ps, 1. . ps 2. .
5. 1 . exec(), ps. ps . ps . .
6. 2 5 . , . - .
7. exec() . - . , wait(). .
|
|
9.
: 㳺 UNIX. .
:
:
− 䳿 UNIX;
− UNIX;
− ;
1.
Linux IPC (Inter-process communication) 䳿 .
IPC:
− UNIX,
− FIFO ( ),
− ,
− ,
− ',
− ,
−
2.
- ' . - IPC, UNIX. ( half-duplex) .
, . , (), (). (fd0), fd1. , , . - , ( ).
in <----- in
Parent Process Kernel Child Process
out <----- out
4
#include <unistd.h> int pipe(int *filedes);
:
filedes[0] - ;
filedes[1] - .
API. .
. .
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <unistd.h>
int mknod(const char *pathname, mode_t, dev_t dev);
' , mode ( S_IFIFO). . - 0, -1.
int unlink(const char *pathname)
.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
int main() {
pid_t childPid;
int flds[2], status;
char buf[]="Message";
//
if (pipe(flds) == -1)
{ perror("Pipe"); exit(1); }
//
switch (childPid=fork())
{ case -1: perror("fork");
exit(2);
case 0:
close(flds[0]);
//
printf("Child process %d\n", getpid()); write(flds[1], buf,
strlen(buf));
close(flds[1]); exit(0); }
// -
printf("Process %d\n", getpid());
close(flds[1]);
read(flds[0], buf, 80);
printf("String -> %s\n", buf);
close(flds[0]);
wait(&status);
return status;
}
(flds[0] flds[1]). , flds[0] , , ' flds[1]. flds[0].
|
|
.
,
mkfifo(NAME, S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO).
, NAME. , . ϳ . ϳ , . unlink(NAME) . ³ ' ( NAME "sfifo.cc").
/* . FIFO */
#include <iostream.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#define NAME "sfifo.cc"
int main() {
int fd;
char buf[80];
unlink(NAME);
if(mkfifo(NAME, S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO)) {
perror(" FIFO");
return 1; }
if((fd=open(NAME, O_RDONLY))==-1) {
perror(" "); }
read(fd, buf, sizeof(buf));
cout<<"->"<<buf<<endl; close(fd);
unlink(NAME);
return 0;
}
- 볺 . ϳ , .
/* 볺 */
#include <iostream.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#define NAME "sfifo.cc"
int main() {
char text[80];
int fd;
cout<<" "<<endl;
cin>>text;
if((fd=open(NAME, O_WRONLY))==-1) {
perror(" 볺");
return 1; }
write(fd, text, strlen(text)); close(fd); return 0;
}
3.
, . 볺, . 볺 . .
1. 볺 . 볺 , ' . 볺 .
2. 볺 , . . .
3. ps, 볺, .
4. 볺 . . , , .
5. 볺 . 볺 , . 볺 ' result.
|
|
6. 볺 . 볺. 볺 . .
7. 볺 . , 볺, .
8. 볺 , . 볺 .
9. 볺 , ' . , . ³ 볺 .
10. 볺 , ' . 볺 , . 볺 .
11. 볺 , ' . , , 볺 . 볺 .
12. 볺 . .
10
: 㳺 UNIX. .
:
:
− UNIX;
− 䳿 ;
− ;
1.
. , , , , . :
− , , 㳺 .
− , ( - ), , , ' .
5 -
IPC , - . , , , , ..
2.
,
key_t ftok(char *filename, char proj);
' ' - , , ' .
.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg);
, IPC_CREAT, (0644). , -1.
int msgsnd(int msgid, struct msgbuf *msgp, int msgsz, int msgflg);
int msgrcv(int msgid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);
. .
struct msgbuf {
long mtype; /* */
char mtext[]; /* */ };
msgsz . msgflg=0 , . msgtyp . - . .
|
|
int msgctl(int msgid, int cmd, struct msgbuf *msgp);
cmd IPC_RMID, NULL.
. . - 볺 . buf mybuf. ( text).
. ' "smess.c". , .
ϳ . 볺 .
cc smess.c o smess. , , smess.
/* */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main()
{key_t key;
struct mybuf { long mtype; char mtext[81];};
struct mybuf buf;
int fd;
char text[81];
int textLen;
printf(" \n");
gets(text);
textLen=strlen(text);
if ((key=ftok("smess.c",0))==-1)
{perror(" "); return 1; }
if ((fd=msgget(key, IPC_CREAT|0644))==-1)
{perror(" ");return 1; }
strncpy(buf.mtext, text, textLen); buf.mtype=1L;
if((fd=msgsnd(fd, &buf, textLen,0))==-1)
{perror(" ");return 1; }
return 0;
}
- 볺 cmess.c.
buf, . ' . , msgget() .
msgrcv() , . .
/* 볺 */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main()
{key_t key;
struct mybuf {long mtype; char mtext[81]; };
struct mybuf buf; int fd; char text[81]; int textLen;
if((key=ftok("smess.c",0))==-1)
{perror(" "); return 1; }
if((fd=msgget(key, 0))==-1)
{perror(" "); return 1; }
if((fd=msgrcv(fd, &buf, 80, 0, 0))==-1)
{ perror(" "); return 1; }
printf(" -> %s\n",buf.mtext);
return 0;
}
- 볺 . Alt+Fn, n - . ϳ . 볺 ( ./cmess). (./smess).
3.
, 볺, . 볺 . 볺 .
1. 볺 . 볺. 볺 . .
2. 볺 . .
3. 볺 . . , , .
4. 볺 . 볺 , ' . 볺 .
5. ps, 볺, .
6. 볺 , ' . , . ³ 볺 .
7. 볺 , ' . , , 볺 . 볺 .
8. 볺 . 볺 , . 볺 ' result.
9. 볺 , ' . 볺 , . 볺 .
10. 볺 . , 볺, .
11. 볺 , . 볺 .
12. 볺 , . . .
˳
1. . UNIX . / ., . - .: ', 1989. - 192 .
2. . Linux: / . , . . - .: ʳ, 1999. - 480 .
3. .. UNIX. - .: BHV--, 1997. - 528 .
4. . UNIX: . - .: , 2003. - 576 .
5. ++ UNIX. .: BHV, 1997. - 592 .
6. ., ., . UNIX. . - ., , 2000. - 368 .
7. . UNIX . / ., . - .: ', 1989. - 192 .
8. . Linux: / . , . . - .: ʳ, 1999. - 480 .
9. .. UNIX. - .: BHV--, 1997. - 528 .
10. . UNIX: . - .: , 2003. - 576 .
11. ++ UNIX. .: BHV, 1997. - 592 .
12. ., ., . UNIX. . - ., , 2000. - 368 .
13. ., . Linux. - -, 2007. - 1104 .
14. . . Linux. - : " ", 2003. - 512 .
15. .., . . Linux: . - ., , 2007 . - 784 .
16. . ., . . Linux. . . - - , 2005. - 392 .
17. . . Linux. . - ., ij, 2002 - 288 .
18. . Unix/Linux. . - ., -, 2004. - 576 .
19. . Linux: . - ., -, 2005. - 656 .
20. . . ++ Linux. - ., -, 2003. - 354 .
21. . Linux . - DiaSoft, 2000. - 488.
22. .. UNIX: . - ., , 2007. - 1039 .
23. . Linux. . - ., ³, 2001.- 464 c.
24. . Linux: . - ., ³, 2001.- 268 c.
25. . Linux. . - ., , 2006. - 432 .
..,
..