LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2004, 03:00 PM   #1
k_wjss
LQ Newbie
 
Registered: Sep 2004
Posts: 12

Rep: Reputation: 0
concurrent running of functions in c prog


how do i actually get 2 functions to run concurrently in a c prog in unix? i am having the problem of having to currently run a user menu that constantly monitors user keyboard input to do the desired task according to user choice. At the same time, i have to constantly update input to my input_buffer_array[] in another function.

How do i get the two functions to run concurrently? The scanf() makes it impossble for the user_menu() function to proceed until a user actually inputs something and press <enter> since it is a buffered keyboard input. My prog will be stuck there for as long as it takes until the user actually inputs something. I do not huv the luxury of getch() or getche() so is there any other way of solving the problem?

Code:
int main(void) {

  char input_buffer_array[100];

  while(1) {
    user_menu( input_buffer_array);

    update_input ( input_buffer_array); // this function will always be delayed
  }

}

void update_input( char input_buffer_array[100]) {

  int i;
  for( i = 0; i < 100; i++) {
    input_buffer_array[i] = ...
    ...
    ...
  }

}

void user_menu( char input_buffer_array[100]) {

  int choice;
  fflush(stdin);
  scanf("%d," &choice);

  ...
  ...
  printf("\n%s\n", input_buffer_array);
  ...

}
 
Old 10-11-2004, 03:10 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
You want to use threading. Basic idea is to create a two pthreads (one that does the user input, one that does the buffer stuff) and start them both off and running.

Look at man pthread_create for info about how to do this.

Also, remember to add the -lpthread switch when you compile your program or it won't work

John
 
Old 10-11-2004, 03:11 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I suppose threads would be the way to go

man pthread_create


Cheers,
Tink

[edit]Heh - too slow ;}[/edit]

Last edited by Tinkster; 10-11-2004 at 03:12 PM.
 
Old 10-11-2004, 03:21 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can fake it by checking the stdin for non-blocking read. Something like this:
Code:
itsme@itsme:~/C$ cat select.c
#include <stdio.h>
#include <sys/select.h>
#include <time.h>

void do_keyboard(void)
{
  char buf[128];

  fgets(buf, sizeof(buf), stdin);
  printf("You typed: %s", buf);
}

void do_time(void)
{
  time_t now = time(NULL);

  printf("Current time is: %s", ctime(&now));
}

int main(void)
{
  struct timeval tv;
  fd_set inpset;

  tv.tv_sec = 5;
  tv.tv_usec = 0;

  while(1)
  {
    FD_ZERO(&inpset);
    FD_SET(fileno(stdin), &inpset);
    select(fileno(stdin)+1, &inpset, NULL, NULL, &tv);

    if(FD_ISSET(fileno(stdin), &inpset))
      do_keyboard();
    else
    {
      do_time();
      tv.tv_sec = 5;
      tv.tv_usec = 0;
    }
  }

  return 0;
}
itsme@itsme:~/C$ gcc -Wall select.c -o select
itsme@itsme:~/C$ ./select
Current time is: Mon Oct 11 13:20:17 2004
Current time is: Mon Oct 11 13:20:22 2004
test
You typed: test
it's running
You typed: it's running
Current time is: Mon Oct 11 13:20:27 2004
as you can see, the Current time is: Mon Oct 11 13:20:32 2004
background function runs uninterrupteCurrent time is: Mon Oct 11 13:20:37 2004
d
You typed: as you can see, the background function runs uninterrupted
Current time is: Mon Oct 11 13:20:42 2004

itsme@itsme:~/C$ ./select
test
You typed: test
Current time is: Mon Oct 11 13:20:49 2004
Current time is: Mon Oct 11 13:20:54 2004
it's kiCurrent time is: Mon Oct 11 13:20:59 2004
nd of working!
You typed: it's kind of working!
Current time is: Mon Oct 11 13:21:04 2004
Current time is: Mon Oct 11 13:21:09 2004
 
Old 10-24-2004, 07:48 PM   #5
k_wjss
LQ Newbie
 
Registered: Sep 2004
Posts: 12

Original Poster
Rep: Reputation: 0
thks for the help!

pthread works however is it possible to suspend the thread and wake it later on? do i make use of signal handlers?
 
Old 10-24-2004, 09:31 PM   #6
emathias
LQ Newbie
 
Registered: Oct 2004
Distribution: Gentoo
Posts: 24

Rep: Reputation: 15
synchronizing threads

Hi,

If you want to stop a thread until the return of another one you can use pthread_join (see more on %man pthread_join ).
If you want to make something more advanced like synchronizing threads you outh to avoid "race conditions" by using mutexes (or semaphores policies)...

Take a look on this tutorial
http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html
It'll help you a little !

Last edited by emathias; 10-24-2004 at 09:32 PM.
 
  


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
kernel 2.4.24 crash after 5-10 days running 1 java prog in JRE1.5.0_04 Yodagold Debian 0 11-08-2005 09:23 AM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 05:37 PM
limiting concurrent webmin duncanbball Linux - Software 0 10-13-2004 03:16 PM
Running functions in the background vexer Programming 6 05-02-2003 01:04 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:54 PM.

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