Sponsored Content
Top Forums Shell Programming and Scripting Deliver values to Thread function Post 302804197 by jimm_the_h on Wednesday 8th of May 2013 07:06:58 AM
Old 05-08-2013
Deliver values to Thread function

Hello,
I hope you can help me with my problem.
I've written a programm which creates a thread. The main programmm creates 10 int and float values. The fucnction should give the values out.
May big problem ist that the delivery to the function is not working.
Where ist my fault?
How can I make easier?
Thank you for your help.

Code:
void funktion1 (void *arg1)
{
	
	printf("Int-Zahl= %i",arg1);
	printf("[TID:%li,PPID:%i] Threadfunktion1 \n",syscall(SYS_gettid),getppid() );
	pthread_exit(NULL);
}

int main()
{	
	struct Zahlen{int z1;float z2;};

	struct Zahlen Zahl;

	pthread_t ptr1, ptr2, ptr3;
	int res1;
	int res2; 
	int bes1;
	int bes2;


	// reine Threadzeile
	res1 = pthread_create(&ptr1, NULL, (void *)&funktion1,(void *)Zahl.z1);

	//Fehlerabfrage Thread3
		if(res1 != 0)
	{
		perror("Thread creation failed!");
	}
	int i;	
	for(i = 0; i<=10;i++)
	{
		Zahl.z1 = random() % 100; //Int Zahl 1-100 erzeugt
		Zahl.z2 =(float)rand() / RAND_MAX; //Float Zahl 0-1 erteugt
		printf("[TID: %li, PID: %i]int-Wert: %i  float-Wert: %f \n",syscall(SYS_gettid), getpid(), Zahl.z1, Zahl.z2);
	}
	bes1 = pthread_join(&ptr1, NULL);
	
	/*if (bes1  !=0) 
		{ 
			perror("Thread join failed");		
			exit(1); 
		}
	return 0;
}


Last edited by Scott; 05-08-2013 at 11:21 AM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. HP-UX

create thread C with JNI function with JAVA

Hello, J create a thread C with a JNI function via JAVA. J have the following message (but not in each time): Someone has an idea ? Thank. Unexpected Signal : 4 occurred at PC=0x78C103E0 Function= Library=(N/A) NOTE: We are unable to locate the function name... (0 Replies)
Discussion started by: AUBERT
0 Replies

2. Programming

create a thread from a returning function

hi all, my requirement is to create a thread by calling another function. i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main. Example: void *thread_function(void *arg) { /* thread function */ int i; rc =... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

3. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

4. Shell Programming and Scripting

passing values to function in Ksh

Hi, I'm trying to work on the script given below #!/bin/ksh -x pfile() { echo "$1" } touch smp19 echo "Hi" > smp19 result=$(pfile $smp19) echo $result As highlighted , when i pass $smp19 as parameter ,it does not display the output.However when i try giving "Hi" instead... (2 Replies)
Discussion started by: Sheema
2 Replies

5. Shell Programming and Scripting

Function returns wrong values - solved

Hi I have a small function which returns a wrong value. The function tries to make a connection to oracle database and tries to get the open_mode of the database in the variable status. However when a database is down the value of the status column is set to READWRITE i am not sure why. I... (0 Replies)
Discussion started by: xiamin
0 Replies

6. Shell Programming and Scripting

how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number i need to capture both and pass it on to unix shell script how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

7. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

8. Programming

Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this: char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

9. Programming

unable to use a function to crate a joinable thread

In my program, threads may be created when some events trigger. So I can't create threads on initialization. Theremore,I write a createThread() function to create thread. However, it is blocking at first call and don't run anymore? why? #include <pthread.h> #include <stdio.h> #include... (4 Replies)
Discussion started by: sehang
4 Replies

10. UNIX for Dummies Questions & Answers

E-mail will not deliver

I am able to send mail to my gmail account and one or two other accounts, but not to my isp e-mail account and another domain I have set up. I am trying to send from VsiFaxServer@ssc.local. And also from root@ssc.local using the mail command at the prompt. When I check the new messages, I found the... (23 Replies)
Discussion started by: smithssc
23 Replies
sigwait(2)							   System Calls 							sigwait(2)

NAME
sigwait - wait until a signal is posted SYNOPSIS
#include <signal.h> int sigwait(sigset_t *set); Standard conforming cc [ flag ... ] file ... -D_POSIX_PTHREAD_SEMANTICS [ library...] #include <signal.h> int sigwait(const sigset_t *set, int *sig); DESCRIPTION
The sigwait() function selects a signal in set that is pending on the calling thread (see thr_create(3C) and pthread_create(3C).) If no signal in set is pending, sigwait() blocks until a signal in set becomes pending. The selected signal is cleared from the set of signals pending on the calling thread and the number of the signal is returned, or in the standard-conforming version (see standards(5)) placed in sig. The selection of a signal in set is independent of the signal mask of the calling thread. This means a thread can synchronously wait for signals that are being blocked by the signal mask of the calling thread . To ensure that only the caller receives the signals defined in set, all threads should have signals in set masked including the calling thread. If the set argument points to an invalid address, the behavior is undefined and errno may be set to EFAULT. If sigwait() is called on an ignored signal, then the occurrence of the signal will be ignored, unless sigaction() changes the disposition. If more than one thread waits for the same signal, only one is unblocked when the signal arrives. RETURN VALUES
Upon successful completion, the default version of sigwait() returns a signal number; the standard-conforming version returns 0 and stores the received signal number at the location pointed to by sig. Otherwise, -1 is returned and errno is set to indicate an error. ERRORS
The sigwait() function will fail if: EINTR The wait was interrupted by an unblocked, caught signal. EINVAL The set argument contains an unsupported signal number. The sigwait() function may fail if: EFAULT The set argument points to an invalid address. EXAMPLES
Example 1 Creating a thread to handle receipt of a signal The following sample C code creates a thread to handle the receipt of a signal. More specifically, it catches the asynchronously generated signal, SIGINT. /******************************************************************** * * compile with -D_POSIX_PTHREAD_SEMANTICS switch; * required by sigwait() * * sigint thread handles delivery of signal. uses sigwait() to wait * for SIGINT signal. * ********************************************************************/ #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <synch.h> static void *threadTwo(void *); static void *threadThree(void *); static void *sigint(void *); sigset_t signalSet; void * main(void) { pthread_t t; pthread_t t2; pthread_t t3; sigfillset ( &signalSet ); /* * Block signals in initial thread. New threads will * inherit this signal mask. */ pthread_sigmask ( SIG_BLOCK, &signalSet, NULL ); printf("Creating threads "); pthread_create(&t, NULL, sigint, NULL); pthread_create(&t2, NULL, threadTwo, NULL); pthread_create(&t3, NULL, threadThree, NULL); printf("################## "); printf("press CTRL-C to deliver SIGINT to sigint thread "); printf("################## "); pthread_exit((void *)0); } static void * threadTwo(void *arg) { printf("hello world, from threadTwo [tid: %d] ", pthread_self()); printf("threadTwo [tid: %d] is now complete and exiting ", pthread_self()); pthread_exit((void *)0); } static void * threadThree(void *arg) { printf("hello world, from threadThree [tid: %d] ", pthread_self()); printf("threadThree [tid: %d] is now complete and exiting ", pthread_self()); pthread_exit((void *)0); } void * sigint(void *arg) { int sig; int err; printf("thread sigint [tid: %d] awaiting SIGINT ", pthread_self()); /* /* use standard-conforming sigwait() -- 2 args: signal set, signum */ err = sigwait ( &signalSet, &sig ); /* test for SIGINT; could catch other signals */ if (err || sig != SIGINT) abort(); printf(" SIGINT signal %d caught by sigint thread [tid: %d] ", sig, pthread_self()); pthread_exit((void *)0); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sigaction(2), signal.h(3HEAD), sigpending(2), sigprocmask(2), sigsuspend(2), pthread_create(3C), pthread_sigmask(3C), thr_create(3C), thr_sigsetmask(3C), attributes(5), standards(5) NOTES
The sigwait() function cannot be used to wait for signals that cannot be caught (see sigaction(2)). This restriction is silently imposed by the system. Solaris 2.4 and earlier releases provided a sigwait() facility as specified in POSIX.1c Draft 6. The final POSIX.1c standard changed the interface as described above. Support for the Draft 6 interface is provided for compatibility only and may not be supported in future releases. New applications and libraries should use the standard-conforming interface. SunOS 5.11 24 Jun 2002 sigwait(2)
All times are GMT -4. The time now is 03:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy