aboutsummaryrefslogtreecommitdiff
path: root/sync
diff options
context:
space:
mode:
Diffstat (limited to 'sync')
-rw-r--r--sync/makefile5
-rw-r--r--sync/reader.c41
-rw-r--r--sync/sync.c60
-rw-r--r--sync/writer.c42
4 files changed, 106 insertions, 42 deletions
diff --git a/sync/makefile b/sync/makefile
new file mode 100644
index 0000000..dd9c463
--- /dev/null
+++ b/sync/makefile
@@ -0,0 +1,5 @@
+sync: sync.c reader.c writer.c
+ cc -o sync sync.c
+ cc -o reader reader.c
+ cc -o writer writer.c
+
diff --git a/sync/reader.c b/sync/reader.c
index 2870d15..66ee57b 100644
--- a/sync/reader.c
+++ b/sync/reader.c
@@ -1,33 +1,40 @@
-#include <sys/types.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <sys/ipc.h>
+#include <sys/select.h>
#include <sys/shm.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
+#include <sys/types.h>
#include <time.h>
-#include <sys/select.h>
+#include <unistd.h>
+
+char *mem;
+void quit(signum)
+int signum;
+{
+ shmdt(mem);
+ exit(1);
+}
int main(argc, argv)
int argc;
char **argv;
{
+printf("\tR STARTING\n");
int shmid, i, pid, id;
- char *mem, filename[50];
+ char filename[50];
FILE *fd;
- struct timeval *s;
- if (argc != 1) {
+ if (argc < 2) {
printf("usage: reader [id]\n");
exit(1);
}
id = atoi(argv[1]);
+printf("\tR%d. started...\n", id);
- if (argc < 2) {
- printf("usage: sync [number readers] [number writers]\n");
- exit(1);
- }
if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){
perror("shmget: shmget failed");
@@ -38,9 +45,12 @@ char **argv;
perror("shmat");
exit(1);
}
+ signal(SIGQUIT, quit);
+printf("\tR%d. SHMID: %d\n", id, shmid);
sprintf(filename, "reader.%d", id);
+printf("\tR%d. opening file \"%s\"...\n", id, filename);
fd = fopen(filename, "a");
if (!fd) {
@@ -50,12 +60,15 @@ char **argv;
srand(time(NULL));
while (1) {
- s->tv_sec = rand() % id;
+printf("\tR%d. reading...\n", id);
for (i = 0; i < 1<<14; i++) {
fprintf(fd, "%c", *(mem + i));
fflush(fd);
}
+ fprintf(fd, "\n");
+ fflush(fd);
- select(0, NULL, NULL, NULL, s);
+printf("\tR%d. waiting...\n", id);
+ sleep(rand() % (id + 1));
}
}
diff --git a/sync/sync.c b/sync/sync.c
index e1fbc4c..66574a1 100644
--- a/sync/sync.c
+++ b/sync/sync.c
@@ -1,16 +1,23 @@
-#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
-#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
-#include <stdio.h>
+
+void quit(signum)
+int signum;
+{
+ shmctl(shmid, IPC_RMID, NULL);
+}
int main(argc, argv)
int argc;
char **argv;
{
- int shmid, i, pid, n_read, n_write;
+ int shmid, i, pid, n_read, n_write, w;
char *mem, **arg_r, **arg_w;
if (argc < 2) {
@@ -29,33 +36,60 @@ char **argv;
perror("shmat");
exit(1);
}
+ signal(SIGQUIT, quit);
+printf("\nSHMID: %d\nsetting initial memory values...\n", shmid);
for (i = 0; i < 1<<14; i++) {
*(mem + i) = 0x30;
}
- arg_r = malloc(sizeof(char*) * 2);
- arg_w = malloc(sizeof(char*) * 2);
+ arg_r = malloc(sizeof(char*) * 3);
+ arg_w = malloc(sizeof(char*) * 3);
+ *arg_r = malloc(sizeof(char) * 10);
+ *arg_w = malloc(sizeof(char) * 10);
+
+ *(arg_r + 1) = malloc(sizeof(char) * 50);
+ *(arg_w + 1) = malloc(sizeof(char) * 50);
- *(arg_r + 1) = NULL;
- *(arg_w + 1) = NULL;
+ *arg_r = "reader";
+ *arg_w = "writer";
+ *(arg_r + 2) = NULL;
+ *(arg_w + 2) = NULL;
+
+printf("forking readers...\n");
for (i = 0; i < n_read; i++){
- sprintf(*arg_r, "%d", i);
+printf("sprintf...\n");
+ sprintf(*(arg_r + 1), "%d", i);
+printf("\t%d\n",i);
if (pid = fork()) {
- printf("starting reader %d...\n", i);
+ /* printf("starting reader %d...\n", i); */
} else {
- execvp("./reader", arg_r);
+ int ret = execv("./reader", arg_r);
+ printf("exec retern %d", ret);
}
}
+printf("forking writers...\n");
for (i = 0; i < n_write; i++) {
- sprintf(*arg_w, "%d", i);
+ sprintf(*(arg_w + 1), "%d", i);
+printf("\t%d\n", i);
if (pid = fork()) {
- printf("starting writer %d...\n", i);
+ /* printf("starting writer %d...\n", i); */
} else {
execvp("./writer", arg_w);
}
}
+
+ shmdt(mem);
+printf("sync done...\n");
+/* TODO
+ * why is this returning 8
+ */
+ for (i = 0; i < (n_write + n_read); i++) {
+ wait(&w);
+ printf("\nReturned with code:%d\n", WTERMSIG(w));
+ }
+ quit();
}
diff --git a/sync/writer.c b/sync/writer.c
index deaa8d0..b1ab9ed 100644
--- a/sync/writer.c
+++ b/sync/writer.c
@@ -1,33 +1,40 @@
-#include <sys/types.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <sys/ipc.h>
+#include <sys/select.h>
#include <sys/shm.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
#include <sys/types.h>
-#include <sys/select.h>
+#include <sys/types.h>
#include <time.h>
+#include <unistd.h>
+char *mem;
+
+void quit(signum)
+int signum;
+{
+ shmdt(mem);
+ exit(1);
+}
int main(argc, argv)
int argc;
char **argv;
{
+printf("\tW STARTING\n");
int shmid, i, pid, id;
char *mem;
- struct timeval *s;
- if (argc != 1) {
- printf("usage: reader [id]\n");
+ if (argc < 2) {
+ printf("usage: writer [id]\n");
exit(1);
}
id = atoi(argv[1]);
- if (argc < 2) {
- printf("usage: sync [number readers] [number writers]\n");
- exit(1);
- }
+printf("\tW%d. started...\n", id);
if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){
perror("shmget: shmget failed");
@@ -38,15 +45,20 @@ char **argv;
perror("shmat");
exit(1);
}
+
+ signal(SIGQUIT, quit);
+printf("\tW%d. SHMID: %d\n", id, shmid);
srand(time(NULL));
-
while (1) {
- s->tv_sec = rand() % (id * 2);
+printf("\tW%d. testing...\n", id);
+ rand() % id;
+printf("\tW%d. writing...\n", id);
for (i = 0; i < 1<<14; i++) {
mem[i]= 0x30 + id;
- select(0, NULL, NULL, NULL, s);
}
+printf("\tW%d. waiting...\n");
+ sleep(rand() % ((id * 2) + 1));
}
}