LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 09-15-2011, 04:54 PM   #1
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Rep: Reputation: 0
Exclamation how can a variable entered in one thread be used in another thread ??


There is a thread1 where I enter an alphabet,
then in thread2 there I have to convert the character to its uppercase/lowercase and print that using a thread3... ??!!

Here is a rough code that I tried :

Code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <ctype.h>

void *input() {
   char chr;
   printf("Enter a character: ");
   scanf("%c", &chr);
}

void *upper_case(void *c) {
   char a;
   a = (char) c;

   if(isupper(a))  printf("\n%c", tolower(a));
   else if(islower(a))  printf("\n%c", toupper(a));
   else printf("\nOMG !");
}

void *print(void *c) {
   char str;
   str = (char) c;

   printf("%c", c);
}

int main(void) {
   pthread_t p1, p2, p3;
   
   pthread_create(&p1, NULL, input, NULL);
   pthread_create(&p2, NULL, upper_case, (void*));
   pthread_create(&p2, NULL, print, (void*));
.
.
.
. 
         I don't know what to do now ?? :(
Please help !!!
 
Old 09-15-2011, 11:09 PM   #2
dgashu
Member
 
Registered: Apr 2010
Location: Banglore
Distribution: fedora , rhel,Ubuntu
Posts: 61

Rep: Reputation: 1
Have a look at following:-
a) Shared variable
b) Pipes
c) Signal

Even an small google search will help you.
 
Old 09-16-2011, 01:41 AM   #3
vamsi9042
LQ Newbie
 
Registered: Sep 2011
Location: Bangalore
Distribution: Ubuntu
Posts: 22

Rep: Reputation: Disabled
[Quote]

Make the letter that you want to read as global variable..
Take a semaphore, and sychronise ur 3 threads using this semaphore busing using sem_wait and sem_post appropriately...

Last edited by vamsi9042; 09-16-2011 at 01:43 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python thread safety: printing from thread to redirected stdout - safe? BrianK Programming 2 10-11-2010 11:28 AM
the return value of getpid() called from main thread and new thread r identical !!! cryincold Programming 3 02-29-2008 01:37 AM
can thread return a local variable? iclinux Programming 1 01-13-2005 11:37 PM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration