Possible bug about thread and semaphore

manuela manuelachessa@tiscali.it
Fri Oct 31 11:13:00 GMT 2003


Hi,
I have a problem with this code with the last version of Cygwin
(I run ipc-daemon2.exe). The program crashes after scanf ( I tested it also
with fgets) if thread timer posted sem2 at least once.
The same code works properly on Linux Suse 7.2 with gcc v. 3.3.1 and Red Hat
with gcc v. 2.9.6.

Is it my fault or Cygwin's fault?

Bye
Manuela

Here follows my code:


#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>

#define DIMBUFFER 20

void send_buffer(char *a);
void * sender(void * arg);
void * timer(void * arg);


sem_t sem1;
sem_t sem2;
char buffer[DIMBUFFER];

void * sender(void * arg)
{
	while(true)
	{
	
		sem_wait(&sem2);
		sem_wait(&sem1);
		send_buffer(buffer);
		buffer[0]='\0';
		sem_post(&sem1);
		
	}
}

void * timer(void * arg)
{
	while(true)
	{
		sleep(5);
		printf("Event\n");
		sem_post(&sem2);
	}
}


int main(int argc, char *argv[])
{
	char string[DIMBUFFER];
	
	buffer[0]='\0';
	int ret;
	pthread_t id;
	pthread_t id2;

	ret = sem_init(&sem1,0,1);
	if(ret<0)
	{
		printf("error");
		exit(1);
	}

	ret = sem_init(&sem2,0,0);
	if(ret<0)
	{
		printf("error");
		exit(1);
	}

	int ret1= pthread_create(&id,NULL,sender,0);
	int ret2= pthread_create(&id2,NULL,timer,0);
	
	
	while(strcmp(string,"*"))
	{
		
		printf("inserisci stringa\n");
		scanf("%s",string);
	

		sem_wait(&sem1);


		strcpy(buffer,string);
		sem_post(&sem1);
		sem_post(&sem2);
		
	}
	return 0;
}

void send_buffer(char *a)
{
	if(strlen(a))
		printf("%s\n",a);
	else
		printf("NULL\n");

}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list