Skip to content

Commit e78f249

Browse files
committed
all
1 parent 0473a51 commit e78f249

File tree

11 files changed

+263
-0
lines changed

11 files changed

+263
-0
lines changed
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
1.94 MB
Binary file not shown.

Makefile

Whitespace-only changes.

common.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//common.c
2+
#include "common.h"
3+
4+
/*
5+
key_t GetKey(const char * pathname)
6+
{
7+
//int fd = open(pathname,O_CREAT,0666);
8+
int fd = open("log.txt",O_RDWR|O_CREAT,S_IRWXU);
9+
if(fd < 0)
10+
{
11+
perror("open file error");
12+
return -1;
13+
}
14+
close(fd);
15+
return ftok(pathname,0);
16+
}
17+
*/
18+
19+
int GetShmId(key_t key)
20+
{
21+
int shmid;
22+
23+
shmid = shmget(key,SHM_SIZE,IPC_CREAT|0666);
24+
if(shmid < 0)
25+
{
26+
perror("Receiver: Shmget Error");
27+
exit(EXIT_FAILURE);
28+
}
29+
30+
return shmid;
31+
}
32+
33+
/*
34+
* create mutex + semaphore
35+
* init those value
36+
*/
37+
void SemInit()
38+
{
39+
/*
40+
* Funtion Prototype:
41+
*
42+
* sem_t *sem_open(const char *name, int oflag,
43+
* mode_t mode, unsigned int value);
44+
*
45+
* name : MUTEX_NAME "mutex_shm"
46+
* oflag : O_CREAT Create and initialize it if not exist
47+
* mode_t : file perssion -rw-r--r--
48+
* value : 1
49+
*/
50+
if((sem_open(MUTEX_NAME,O_CREAT,0644,1)) < 0)
51+
{
52+
perror("sem_open");
53+
exit(EXIT_FAILURE);
54+
}
55+
56+
if((sem_open(FULL_NAME,O_CREAT,0644,0)) < 0){
57+
perror("sem_open");
58+
exit(EXIT_FAILURE);
59+
}
60+
}
61+
62+
63+
/*
64+
* close and unlink semaphore that we crated
65+
*/
66+
void SemDestroy()
67+
{
68+
sem_t * mutexPtr = sem_open(MUTEX_NAME,O_CREAT);
69+
sem_t * fullPtr= sem_open(FULL_NAME,O_CREAT);
70+
71+
/* Destroy mutex */
72+
sem_close(mutexPtr); // int sem_close(sem_t *sem);
73+
sem_unlink(MUTEX_NAME); // int sem_unlink(const char *name);
74+
75+
/* Destory full*/
76+
sem_close(fullPtr);
77+
sem_unlink(FULL_NAME);
78+
}
79+
80+
81+
void P(sem_t *semPtr)
82+
{
83+
sem_wait(semPtr); //int sem_wait(sem_t *sem);
84+
}
85+
86+
void V(sem_t *semPtr)
87+
{
88+
sem_post(semPtr); //int sem_post(sem_t *sem);
89+
}

common.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//common.h
2+
#ifndef _COMMON_H_
3+
#define _COMMON_H_
4+
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
#include <sys/stat.h>
11+
#include <fcntl.h>
12+
#include <pthread.h>
13+
#include <semaphore.h>
14+
15+
#include <sys/types.h>
16+
17+
18+
#include <unistd.h>
19+
20+
#include <sys/ipc.h>
21+
#include <sys/shm.h>
22+
23+
24+
static const char * MUTEX_NAME = "mutex_shm";
25+
static const char * FULL_NAME = "full_shm";
26+
//static const char * PATH_NAME = "tmp/shmtemp";
27+
28+
29+
//constant define
30+
#define SHM_SIZE 1024
31+
32+
#define KEY_NUM 1000
33+
34+
/*
35+
key_t GetKey(const char * pathname);
36+
*/
37+
38+
int GetShmId(key_t key);
39+
/*
40+
* create mutex + semaphore
41+
* init those value
42+
*/
43+
void SemInit();
44+
45+
void SemDestroy();
46+
47+
void P(sem_t *sem);
48+
49+
void V(sem_t *sem);
50+
51+
#endif

init.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//init.c
2+
#include "common.h"
3+
4+
int main(int argc, char const *argv[])
5+
{
6+
key_t key;
7+
int semid; //semaphore id
8+
int shmid; //shared memory id
9+
10+
11+
/* Create key*/
12+
key = KEY_NUM;
13+
14+
/* Initialize Semaphore*/
15+
SemInit();
16+
17+
/* TODO Initialize Shared Memory*/
18+
GetShmId(key);
19+
20+
printf("End of initialize\n");
21+
return 0;
22+
}

receiver.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//receiver.c
2+
#include "common.h"
3+
//key
4+
key_t key;
5+
6+
//shared memory
7+
int shmid;
8+
char * shmptr;
9+
char result[SHM_SIZE];
10+
11+
//semaphore
12+
sem_t * full;
13+
sem_t * mutex;
14+
//semaphore
15+
16+
17+
void Init()
18+
{
19+
key = KEY_NUM; //init key
20+
shmid = GetShmId(key); // init shared memory
21+
shmptr = shmat(shmid,NULL,0); // attach segement to vitural ...?
22+
//semaphore init
23+
full = sem_open(FULL_NAME,O_CREAT);
24+
mutex = sem_open(MUTEX_NAME,O_CREAT);
25+
}
26+
27+
void ReadMessage()
28+
{
29+
P(full);
30+
P(mutex);
31+
strcpy(result,shmptr);
32+
V(mutex);
33+
}
34+
35+
int main(int argc, char const *argv[])
36+
{
37+
38+
39+
Init();
40+
41+
/*waiting for user to input message*/
42+
ReadMessage();
43+
44+
printf("Receiver : message is %s\n",result);
45+
SemDestroy();
46+
printf("Receiver : Process End \n");
47+
return 0;
48+
}

sender.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//sender.c
2+
#include "common.h"
3+
#include <stdio.h>
4+
5+
//key
6+
key_t key;
7+
8+
//shared memory
9+
int shmid;
10+
char * shmptr;
11+
char input[SHM_SIZE];
12+
13+
//semaphore
14+
sem_t * full;
15+
sem_t * mutex;
16+
//semaphore
17+
18+
19+
void Init()
20+
{
21+
key = KEY_NUM; //init key
22+
shmid = GetShmId(key); // init shared memory
23+
shmptr = shmat(shmid,NULL,0); // attach segement to vitural ...?
24+
//semaphore init
25+
full = sem_open(FULL_NAME,O_CREAT);
26+
mutex = sem_open(MUTEX_NAME,O_CREAT);
27+
}
28+
29+
void SaveMessage()
30+
{
31+
32+
P(mutex);
33+
strcpy(shmptr,input);
34+
V(mutex);
35+
36+
V(full);
37+
}
38+
39+
int main(int argc, char const *argv[])
40+
{
41+
42+
43+
Init();
44+
45+
/*waiting for user to input message*/
46+
47+
fgets(input, 1024, stdin); //input message from shell
48+
49+
SaveMessage();
50+
51+
printf("Sender: Process End\n");
52+
return 0;
53+
}

0 commit comments

Comments
 (0)