Skip to content

Commit 1907551

Browse files
author
Maciej Andrearczyk
committed
Nauka kolejek komunikatow
1 parent e020e43 commit 1907551

6 files changed

Lines changed: 139 additions & 4 deletions

File tree

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
CFLAGS=-Wall -std=c99
22
S=serwer.c
33
K=klient.c
4+
E=err.c
45

56
all: serwer klient
67

7-
serwer: $(S)
8-
gcc $(CFLAGS) $(S) -o serwer
8+
serwer: $(S) $(E)
9+
gcc $(CFLAGS) $(S) $(E) -o serwer
910

10-
klient: $(K)
11-
gcc $(CFLAGS) $(K) -o klient
11+
klient: $(K) $(E)
12+
gcc $(CFLAGS) $(K) $(E) -o klient
1213

1314
clean:
1415
rm -f serwer klient

err.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <stdarg.h>
5+
#include <errno.h>
6+
#include <sys/types.h>
7+
#include <sys/wait.h>
8+
#include <string.h>
9+
#include "err.h"
10+
11+
extern int sys_nerr;
12+
13+
void syserr(const char *fmt, ...)
14+
{
15+
va_list fmt_args;
16+
17+
fprintf(stderr, "ERROR: ");
18+
19+
va_start(fmt_args, fmt);
20+
vfprintf(stderr, fmt, fmt_args);
21+
va_end (fmt_args);
22+
fprintf(stderr," (%d; %s)\n", errno, strerror(errno));
23+
exit(1);
24+
}
25+
26+
void fatal(const char *fmt, ...)
27+
{
28+
va_list fmt_args;
29+
30+
fprintf(stderr, "ERROR: ");
31+
32+
va_start(fmt_args, fmt);
33+
vfprintf(stderr, fmt, fmt_args);
34+
va_end (fmt_args);
35+
36+
fprintf(stderr,"\n");
37+
exit(1);
38+
}

err.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef _ERR_
2+
#define _ERR_
3+
4+
/* wypisuje informacje o blednym zakonczeniu funkcji systemowej
5+
i konczy dzialanie */
6+
extern void syserr(const char *fmt, ...);
7+
8+
/* wypisuje informacje o bledzie i konczy dzialanie */
9+
extern void fatal(const char *fmt, ...);
10+
11+
#endif

klient.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
//Maciej Andrearczyk, 333856
22
#include "stdio.h"
33
#include "stdlib.h"
4+
#include "err.h"
5+
#include "mesg.h"
6+
#include <unistd.h>
7+
#include <string.h>
8+
#include <sys/types.h>
9+
#include <sys/ipc.h>
10+
#include <sys/msg.h>
11+
#include <sys/stat.h>
412

513
/*
614
* Rodzaj zasobu.
@@ -17,10 +25,41 @@ int n;
1725
*/
1826
int s;
1927

28+
/*
29+
* Id kolejki to wysylania komunikatow.
30+
*/
31+
int snd_qid;
32+
33+
/*
34+
* Id kolejki to odbierania komunikatow.
35+
*/
36+
int rcv_qid;
37+
38+
/*
39+
* Wiadomość z żądaniem.
40+
*/
41+
Msg req;
42+
2043
int main(int argc, char* argv[])
2144
{
2245
k = atoi(argv[1]);
2346
n = atoi(argv[2]);
2447
s = atoi(argv[3]);
2548

49+
req.msg_type = 1L;
50+
51+
printf("MOJ PID %d\n", getpid());
52+
sprintf(req.data, "%d %d %d", n, k, getpid());
53+
54+
if ( (snd_qid = msgget(CLI_TO_SER, 0)) == -1)
55+
syserr("Error in msgget\n");
56+
57+
if ( (rcv_qid = msgget(SER_TO_CLI, 0)) == -1)
58+
syserr("Error in msgget\n");
59+
60+
if ( msgsnd(snd_qid, (char *) &req, strlen(req.data), 0) != 0 )
61+
syserr("Error in msgsnd");
62+
63+
printf("KONIEC\n");
64+
exit(0);
2665
}

mesg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#define MAX_DATA_SIZE 1000
2+
3+
typedef struct {
4+
long msg_type;
5+
char data[MAX_DATA_SIZE];
6+
} Msg;
7+
8+
#define CLI_TO_SER 100
9+
#define SER_TO_CLI 101

serwer.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
//Maciej Andrearczyk, 333856
22
#include "stdio.h"
33
#include "stdlib.h"
4+
#include "signal.h"
5+
#include "err.h"
6+
#include "mesg.h"
7+
#include <sys/types.h>
8+
#include <sys/ipc.h>
9+
#include <sys/msg.h>
10+
#include <sys/stat.h>
411

512
/*
613
* Ilość rodzajów zasobów.
@@ -12,9 +19,39 @@ int K;
1219
*/
1320
int N;
1421

22+
/*
23+
*TODO
24+
*/
25+
int msg_qid;
26+
Msg msg;
27+
28+
29+
/*
30+
* Metoda zwalniająca zasoby systemowe.
31+
*/
32+
void free_sysres()
33+
{
34+
printf("\nZWALNIAM ZASOBY!\n");
35+
if (msgctl(msg_qid, IPC_RMID, 0) == -1)
36+
syserr("Error in msgctl\n");
37+
exit(0);
38+
}
39+
1540
int main(int argc, char* argv[])
1641
{
1742
K = atoi(argv[1]);
1843
N = atoi(argv[2]);
1944

45+
////
46+
if ( (msg_qid = msgget(1234L, IPC_CREAT | IPC_EXCL)) == -1)
47+
syserr("Error in msgget\n");
48+
////
49+
int size_rcv;
50+
while((size_rcv = msgrcv(msg_qid, &msg, MAX_DATA_SIZE, 1L, 0))) {
51+
printf("ALE %s\n", msg.data);
52+
if ( signal(SIGINT, free_sysres) == SIG_ERR)
53+
syserr("Error in signal\n");
54+
}
55+
56+
exit(0);
2057
}

0 commit comments

Comments
 (0)