Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/msg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <time.h>
- #define MSG_TEXT_SIZE 1024
- int msg_id = -1;
- struct MSGBuf {
- long msgType;
- char msgText[MSG_TEXT_SIZE];
- };
- int printErrorAndReturn(const char* errorString){
- perror(errorString);
- return EXIT_FAILURE;
- }
- int forkMain(){
- int pid = getpid();
- for(;;){
- struct MSGBuf buf;
- long msgTypeFilter = pid;
- if(msgrcv(msg_id, &buf, MSG_TEXT_SIZE, msgTypeFilter, 0) == -1){
- puts("ERROR");
- break;
- }
- if(buf.msgText[0] == '#')
- break;
- printf("%d: %s", pid, buf.msgText);
- fflush(stdout);
- }
- return EXIT_SUCCESS;
- }
- int main(const int argc, const char** argv){
- if(argc != 2){
- printf("Usage %s: <number of proc>\n", argv[0]);
- return 1;
- }
- int nmbrOfProc = atoi(argv[1]);
- if((msg_id = msgget(IPC_PRIVATE, IPC_CREAT | 0600)) == -1) return printErrorAndReturn("msgget");
- int pids[1024*1024] = {-1};
- size_t pidsCounter = 0;
- for(int i=0; i<nmbrOfProc; i++){
- if((pids[pidsCounter] = fork()) == 0)
- return forkMain();
- else if(pids[pidsCounter] == -1) return printErrorAndReturn("FORK");
- pidsCounter++;
- }
- for (size_t i = 0; i < pidsCounter; i++)
- printf("PID: %d\n", pids[i]);
- while(1){
- puts("\nPodaj tekst:");
- char inputText[MSG_TEXT_SIZE];
- if(fgets(inputText, MSG_TEXT_SIZE, stdin) == NULL){
- puts("fgets error!!!");
- break;
- }
- int choosenPid = -1;
- if(inputText[0] != '#'){
- puts("Podaj pid nadawcy:");
- int goodPid = 0;
- do{
- while(scanf("%d", &choosenPid) != 1)
- { puts("wpisz poprawny pid!"); }
- size_t i;
- for (i = 0; i < pidsCounter; i++){
- if(choosenPid == pids[i]){
- goodPid = 1;
- break;
- }
- }
- }
- while(goodPid == 0);
- struct MSGBuf buf;
- buf.msgType = choosenPid;
- memcpy(buf.msgText, inputText, MSG_TEXT_SIZE);
- if(msgsnd(msg_id, &buf, MSG_TEXT_SIZE, 0) == -1)
- return printErrorAndReturn("msgsnd");
- }
- else{
- size_t i=0;
- for(i=0; i<pidsCounter; i++){
- struct MSGBuf buf;
- buf.msgType = pids[i];
- memcpy(buf.msgText, inputText, MSG_TEXT_SIZE);
- if(msgsnd(msg_id, &buf, MSG_TEXT_SIZE, 0) == -1)
- return printErrorAndReturn("msgsnd");
- }
- break;
- }
- fflush(stdout);
- getc(stdin);
- usleep(10000);
- }
- msgctl(msg_id, IPC_RMID, NULL);
- }
- /*
- Na poczatku dolaczamy potrzebne biblioteki. Przy wykonywaniu programu podajemy parametr odpowiedzialny za liczbe procesow. Tworzymy kolejke komunikatow
- przy pomocy funkcji msgget. Nastepnie tworzymy tyle procesow ile podalismy w argumencie programu. Kazdy z nowo utworzonych procesow czyta dane z kolejki i
- wypisuje je na wyjscie poprzedzajac swoim pidem. Proces macierzysty wypisuje pidy dzieci a nastepnie w pętli prosi o podanie tresci i pidu do ktorego
- ma wyslac komunikat. Znak # konczy prace programu.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement