public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Possible bug about thread and semaphore
@ 2003-10-31 11:13 manuela
  0 siblings, 0 replies; only message in thread
From: manuela @ 2003-10-31 11:13 UTC (permalink / raw)
  To: cygwin

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/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-10-31 10:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-31 11:13 Possible bug about thread and semaphore manuela

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).