Skip to content

Instantly share code, notes, and snippets.

@nietaki
Created December 20, 2010 18:22
Show Gist options
  • Save nietaki/748758 to your computer and use it in GitHub Desktop.
Save nietaki/748758 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include "err.h"
#include <limits.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <assert.h>
#include <pthread.h>
#include "msglib.h"
int N;
int inqid, outqid;
int free_types;
int types[201];
int curtype;
pthread_mutex_t types_mut;
pthread_cond_t types_cond;
pthread_attr_t attr;
void *watek(void *intype_ptr){
printf("samo wnetrze watku \n");
int intype = *((int*)intype_ptr);
free((int*)intype_ptr);
printf("%i - intype\n", intype);
int k = intRcv(inqid, intype);
printf("rozmiar wiadomosci do odebrania - %i \n", k);
int i, tmp;
/*for(i=0;i<k;i++){
tmp = intRcv(inqid, intype);
printf("odebralem Z_%i: %i", i, tmp);
}*/
return 0;
}
void resetuj(){
int i;
for(i = 1; i <= 201; i++){
types[i] = FREE;
}
free_types = 200;
curtype = 1;
if (pthread_mutex_init(&types_mut, 0) != 0){
syserr("types_mut mutex init");
}
if (pthread_cond_init(&types_cond, NULL) != 0){
syserr("types_cond cond init");
}
}
void mylock(pthread_mutex_t *mut){
if (pthread_mutex_lock(mut) != 0){
syserr("mylock");
}
}
void myunlock(pthread_mutex_t *mut){
if (pthread_mutex_unlock(mut) != 0){
syserr("myunlock");
}
}
void mywait(pthread_cond_t *cond, pthread_mutex_t *mutex){
if (pthread_cond_wait(cond, mutex) != 0){
syserr("cond wait");
}
}
void mysignal(pthread_cond_t *cond){
if (pthread_cond_signal(cond) != 0){
syserr("cond signal");
}
}
int getType(){
mylock(&types_mut);
while(free_types <1){
mywait(&types_cond, &types_mut);
}
curtype = 1;
while(types[curtype] != FREE){
curtype = (curtype % 200) + 1; //1..200
}
types[curtype] = USED;
myunlock(&types_mut);
return curtype;
}
void returnType(int intype){
mylock(&types_mut);
if(types[intype] == FREE){
fatal("proba oddania wolnego typu");
}
types[intype] = FREE;
free_types++;
myunlock(&types_mut);
//TODO sprawdzic czy tutaj, czy przed oddaniem muteksa
mysignal(&types_cond);
}
void stworzAttr(){
int err;
if((err = pthread_attr_init(&attr)) != 0)
syserr("attrinit");
if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) != 0)
syserr("setdetach");
printf("stworzono attr \n");
}
int stworzWatek(int intype){
printf("%i - intype w stworzWatek \n", intype);
//staramy sie, zeby nam argument nie uciekl zanim przejmiemy go w watku
int* myintype = (int*) malloc(sizeof(int));
*myintype = intype;
int err;
pthread_t thr;
if((err = pthread_create(&thr, &attr, watek, myintype /*&intype*/)) != 0)
syserr("create");
return 0;
}
int main (int argc, char *argv[])
{
int n[ARR_MAX + 1 ];
resetuj(n);
stworzAttr();
if(argc < 2 ){
syserr("za malo argumentow");
}else if(atoi(argv[1]) == 0){
syserr("niepoprawny argument");
}
N = atoi(argv[1]);
assert(sizeof(pid_t) == sizeof(int));
inqid = getMsgId(1, K_KEY);
outqid = getMsgId(1, S_KEY);
printf("zaczynam odbierac\n");
//printf("%s\n\n\n", tmp.mtext);
int curpid;
int tmptype;
while(1){
printf("petla\n");
//odbieram pid
curpid = intRcv(inqid, 666);
tmptype = getType();
printf("wysylam klientowi numer typu do komunikacji: %i\n", tmptype);
intSnd(outqid, 777, tmptype);
//chyba nic wiecej
//watek(&curpid);
stworzWatek(tmptype);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment