Jump to content
michee

scriere concurenta intr-un fisier in C

Recommended Posts

Am programul aceasta:



#include <arpa/inet.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include "http_get.c"
#include "build.h"


#define DEBUG 0 /* change this to 1 for debugging.. */

FILE *infile, *outfile;
int sock, numforks = 0, timesup = 0, port = 21;


int main(int argc, char *argv[])
{
char buf[1024];
time_t start;
char *file = "out.txt";
int i,j;
int max_forks =1000, num_procs = 0;

numforks=0;
//outfile = fopen(file, "w");

j= open(file,O_RDWR);
start = time(0);

for(i=1;i<=100000;i++)
{

if (!(fork()))
{
sprintf(buf,"Sunt procesul cu pid-ul %d \n",getpid());
write(j,buf, strlen(buf));
exit(0);
}
else
{
numforks++;
num_procs++;
if (numforks > max_forks)
for (numforks; numforks > max_forks; numforks--)
wait(NULL);
}
}
close(j);
//fclose(outfile);
printf("Done! completed in %lu seconds, procese %d\n", (time(0) - start),num_procs);
exit(EXIT_SUCCESS);
}

Ar trebui sa creeze 100k procese care sa scrie intr-un fisier, maxim 1000 odata. si outputul fisierului sa fie destul de amestecat (zic eu) ptr ca nu exista nici un fel de locking. Cu toate acestea fisierul e foarte ok, nu e nimik amestecat(varza), sunt aproximativ 97 k linii.

De ce se comporta ca si cum ar fi locking desi nu exista locking?

De ce sunt doar 97 k linii si nu 100k?

Pe ultima linie parintele zice ca a facut 100k procese.

Mersi

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...