hi,
I've got a problem with that program. I wan't to make program which writes byte by byte to file, after written every sign is read and written to another file. How have i to organise semaphores? I gave to both semaphores value 0 in the beggining. thanks for help
I've got a problem with that program. I wan't to make program which writes byte by byte to file, after written every sign is read and written to another file. How have i to organise semaphores? I gave to both semaphores value 0 in the beggining. thanks for help
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include <unistd.h>
#include<stdlib.h>
struct sembuf buf;
void down(int semid, short semnum)
{
int sem;
buf.sem_num=semnum;
buf.sem_op=1;
buf.sem_flg=0;
if((sem=semop(semid, &buf, 1))==-1)
{
printf("Blad semop w podnies\n");
return;
}
}
void up(int semid, short semnum)
{
int sem;
buf.sem_num=semnum;
buf.sem_op=-1;
buf.sem_flg=0;
if((sem=semop(semid, &buf, 1))==-1)
{
printf("Blad semop w opusc\n");
return;
}
}
////////////////////////
fk=fork();
if(fk>0)
{
while(buf[i]!='\0')
{
fp=fopen("plik", "aw");
fseek(fp, i, SEEK_SET);
fwrite(&buf[i], 1, 1, fp);
fclose(fp);
printf("%d\n", i);
i++;
up(semid,1);
down(semid, 0);
}
}
else if(fk==0)
{
char buf1[100];
int j=0;
while(j<=a)
{
down(semid, 1);
fp=fopen("plik", "r");
fseek(fp, j, SEEK_SET);
fread(&buf1[j], 1, 1, fp);
fclose(fp);
fr=fopen("plik1", "aw");
fseek(fr, j, SEEK_SET);
fwrite(&buf1[j], 1, 1, fr);
fclose(fr);
j++;
up(semid, 0);
}