#include <iostream> #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> #include <unistd.h> #include <cstring> #include <termios.h> #define KEY 1234 union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO*/ }; static int WaitSemaphore(int sem_id, int sem_num) { struct sembuf semBuf; semBuf.sem_num = sem_num; semBuf.sem_op = -1; semBuf.sem_flg = IPC_NOWAIT; if(semop(sem_id, &semBuf, 1) == -1) { std::cout << "Can't done semaphore P: "; perror("semop"); std::cout << std::endl; exit(0); } return (1); } static int ReleaseSemaphore(int sem_id, int sem_num) { struct sembuf semBuf; semBuf.sem_num = sem_num; semBuf.sem_op = 1; semBuf.sem_flg = IPC_NOWAIT; if(semop(sem_id, &semBuf, 1) == -1) { std::cout << "Can't done semaphore V: "; perror("semop"); std::cout << std::endl; exit(0); } return (1); } int main() { static int semId; int dataProcessed; int filePipes[2]; char someData[100]; char buffer[BUFSIZ + 1]; pid_t forkResult; system("clear"); union semun semopts; semopts.val = 1; for(int i = 0; i < 2; i++) if (semctl(semId, i, SETVAL, 0) == -1) return(0); if((semId = semget((key_t)KEY, 0, 0666)) < 0) { perror("semget"); exit(-1); } while(true) { if(pipe(filePipes) == 0) { forkResult = fork(); if(forkResult == (pid_t)-1) { std::cout << "Fork failure: " << stderr; exit(EXIT_FAILURE); } if(forkResult == 0) //client { WaitSemaphore(semId, 0); memset(buffer, 0, sizeof(buffer)); //заполнить нулями dataProcessed = read(filePipes[0], buffer, BUFSIZ); std::cout << "Client: "; for(int i = 0; i < BUFSIZ + 1; i++) { std::cout << buffer[i]; } sleep(1); //std::cout << "Process ID:" << getpid() << " read " << dataProcessed << " bytes" << std::endl; ReleaseSemaphore(semId, 1); } else //server { ReleaseSemaphore(semId, 0); std::cout << "\nServer: "; std::cin >> someData; dataProcessed = write(filePipes[1], someData, strlen(someData)); //std::cout << "Process ID:" << getpid() << " wrote " << dataProcessed << " bytes" << std::endl; WaitSemaphore(semId, 1); } } } semctl(semId, 0, IPC_RMID, 0); waitpid(forkResult, NULL, 0); system("clear"); return 0; } TASK: It is necessary to create two processes: client and server. The server process waits for the user to enter a text string and the following actions are initiated by pressing the Enter key: the client process waits for notification that the server process is ready to start data transfer (synchronization); the server process transfers the string received from the user to the client process using either channels or shared memory segments / file projections; the client process accepts a string and displays it on the screen; the server process waits for a notification from the client process about the successful receipt of the string; the server process waits for the user to enter the string again, and so on. This work continues mastering the synchronization of processes. Notification of processes must be done by means of semaphores.
I have some small mistake, I can not understand where! 2 days racking my head! Clips out only TERM environment variable not set. Although before that everything worked, except for semaphores! Maybe I wrote the semaphores incorrectly, but I only learn and cannot understand such complex things right away. Especially working with multiple semaphores in Linux.
TERMvariable to some suitable value before runningeclipse. those.export TERM=linux, for example. it's in the console. Your application callsclear, system, and it is sure to find out how to clear your terminal trying to get the value of theTERMenvironment variable. Although I am surprised why it is not set for you, most ways in which you can connect to linux form a normalTERM- Mike