Академический Документы
Профессиональный Документы
Культура Документы
Томск – 2008
ЦЕЛЬ РАБОТЫ: Практическое знакомство с объектом процесс, основными
механизмами передачи данных между процессами, а также синхронизацией
взаимодействующих процессов в ОС Unix.
[root:~]# ps
PID TTY STAT TIME COMMAND
41 v01 S 0:12 -bash
42 v02 S 0:00 /sbin/getty tty2 VC console
43 v03 S 0:00 /sbin/getty tty3 VC console
44 v04 S 0:00 /sbin/getty tty4 VC console
142 v01 R 0:01 ps
[root:~]# ps -aux
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
root 1 0.0 0.6 44 192 ? S 19:13 0:03 init
root 41 0.0 1.5 360 472 v01 S 19:15 0:13 -bash
root 42 0.0 0.6 56 192 v02 S 19:15 0:00 /sbin/getty tty2 VC c
root 22 0.0 0.7 68 240 ? S 19:15 0:01 /usr/sbin/crond
root 17 0.0 0.7 54 228 ? S 19:15 0:02 /sbin/syslogd
root 19 0.0 0.6 68 212 ? S 19:15 0:01 /usr/sbin/inetd
root 24 0.0 0.5 64 172 ? S 19:15 0:00 /usr/sbin/lpd
root 27 0.0 1.0 288 324 ? S 19:15 0:07 /usr/lib/sendmail -q1
root 43 0.0 0.6 56 192 v03 S 19:15 0:00 /sbin/getty tty3 VC c
root 44 0.0 0.6 56 192 v04 S 19:15 0:00 /sbin/getty tty4 VC c
root 31 2.0 0.2 5 68 ? S 19:15 5:57 /sbin/update
root 143 0.0 0.7 72 224 v01 R 23:59 0:01 ps -aux
[root:~]#
[root:~]# ./q2 &
[1] 144
[root:~]# jobs
[1]+ Running ./q2 &
[root:~]# fg
./q2
[root:~]# ./q2
2. Межроцессное взаимодействие.
Текст программы:
#include <stdio.h>
#include <cerrno>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
//Semaphore
int semid = semget(SKEY,1,oflag);
arg.buf = &seminfo;
arg.val = 1;
semctl(semid, 0, SETVAL, arg);
//Memory
char *pointer;
struct shmid_ds sharedMemoryBuf;
// lock
psmb.sem_num = 0;
psmb.sem_flg = 0;
psmb.sem_op = -1;
semop(semid, &psmb, 1);
//Child_____________________
pid_t pid;
if ((pid = fork()) == 0)
{
//lock
int flag = SVSEM_MODE|IPC_EXCL;
int smd = semget(SKEY, 1, flag);
struct sembuf csmb;
csmb.sem_num = 0;
csmb.sem_flg = 0;
csmb.sem_op = -1;
semop(semid, &csmb, 1);
}
printf ("Sum= "); printf ("%i",sum); printf(" ");
// unlock
csmb.sem_num = csmb.sem_flg = 0;
csmb.sem_op = 1;
semop(smd, &csmb, 1);
exit(0);
}
//End of child process
//Memory filling
int num=rand()%10;
*pointer++ = (char)num;
//unlock
psmb.sem_num = psmb.sem_flg = 0;
psmb.sem_op = 1;
semop(semid,&psmb,1);
exit(0);
}
В общем виде работу программы можно описать следующим образом.
Родительский процесс блокирует семафор, заполняет память, разблокирует
семафор и дожидается окончания дочернего процесса, дочерний процесс
извлекает данные из памяти и находит сумму.
Результат выполнения.
[root:~]# ./b
Getted numbers:9 4 1 8 7 5 2 3 3 8 Sum= 50 [root:~]#
Тексты программ:
Файл cf.cpp
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream.h>
#include <strings.h>
#include <fstream.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define MAXLINE 128
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
int main() {
int readfd = -1,writefd = -1;
pid_t childpid = 0;
size_t n = 0;
char str[MAXLINE];
sleep(1);
cout <<str;
break;
}
cout<< str;
close(readfd);
close(writefd);
return 1;
}
Файл sf.cpp
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <cerrno>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <strings.h>
#include <iostream.h>
#include <error.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
int main() {
char str[MAXLINE];
ssize_t n;
int readfd = -1,writefd = -1;
unlink(FIFO1);
unlink(FIFO2);
if (mkfifo(FIFO1, FILE_MODE) == EEXIST) cout<<"\n Pipes is exists"<<endl;
if (mkfifo(FIFO2, FILE_MODE) == EEXIST) cout<<"\n Pipes is exists"<<endl;
break;
}
close(readfd);
close(writefd);
unlink(FIFO1);
unlink(FIFO2);
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <error.h>
#include <fcntl.h>
#include <stdio.h>
#include <cerrno>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <strings.h>
#include <iostream.h>
struct {
long mtype;
char mtext[100];
} ptr;
int main()
{
char text[100];
gets(text);
ptr.mtype = 1;
strcpy(ptr.mtext,text);
msgsnd(mqid, (struct msgbuf*)(&ptr),strlen(text)+1, 0);
msgctl(mqid,IPC_RMID,NULL);
return 0;
}
Файл q2
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <cerrno>
#include <iostream.h>
struct {
long mtype;
char mtext[100];
} ptr;
int main()
{
flag = 0;
type =1;
char text[MAXMSG];
strcpy(text,ptr.mtext);
switch(num) {
case 90: text[i]= 'A'; break;
case 122: text[i]='a'; break;
}
cout<< text;
return 0;
}
Результат выполнения.