Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <pqueue.h>
- #include <time.h>
- #define PQUEUE_H
- char *filename = "queue.dat";
- int id=0;
- int *itemId = &id;
- int bufforSize=3;
- typedef struct item item;
- struct item {
- int id;
- };
- item *
- produce() {
- time_t t;
- int timeX = rand() % 5 + 1;
- item *it = (item *)malloc(sizeof(item));
- sleep(timeX);
- it->id = *itemId;
- (*itemId) += 1;
- return it;
- }
- void
- consume(item *it) {
- time_t t;
- int timeX = rand() % 5 + 1;
- sleep(timeX);
- *itemId -=1;
- printf("-----%d-----\n",*itemId);
- free(it);
- }
- void
- producer(pid_t childPid, pqueue **phead) {
- if(*itemId != bufforSize){
- item* producedElem = produce();
- int k = rand() % 100 + 1;
- printf("Im in producer!\n");
- qinsert(phead, producedElem, k);
- printf("%d\n",(*phead)->k);
- qserialize(*phead, sizeof(producedElem), filename);
- }
- else
- {
- printf("########################## ELSE ########################## \n");
- sleep(5);
- kill(childPid, SIGSTOP);
- }
- //qserialize(pqueue *head, size_t ndata, char *filename);
- //otworz plik, wpisz, wyjd
- }
- void
- consumer(pqueue **phead) {
- //qpop()
- printf("Hi, consumere here!\n");
- //qunserialize()
- //qunserialize(&qu, sizeof(item), filename);
- //consume();
- }
- int
- main(int argc, char **argv) {
- time_t t;
- srand(time(0));
- pid_t pid;
- pqueue *qu = NULL;
- int ifBuffFree = 1;
- qserialize(qu, sizeof(item), filename);
- int forkRes=fork();
- while(1){
- if(forkRes==0)
- {
- printf("Child here! Lets go to producer!\n");
- if(*itemId== BUFSIZ){ ifBuffFree=0;}
- producer(getpid(),&qu);
- printf("-----%d-----\n",*itemId);
- sleep(3);
- }
- else if(forkRes>0)
- {
- printf("Parent here! Lets go to consumer!\n");
- //while(!)
- qunserialize(&qu, sizeof(item), filename);
- consumer(&qu);
- qserialize(qu, sizeof(item), filename);
- //qunserialize(&qu, sizeof(item), filename);
- //qunserialize(pqueue **phead, size_t ndata, char *filename);
- sleep(3);
- }
- else
- {
- printf("Aaaaa");
- }
- }
- /* watch -n 1 ps -l --forest */
- /* create empty queue */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement