|
Home > Archive > Unix Programming > August 2006 > problem with the program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problem with the program
|
|
| Subhash 2006-08-22, 1:39 am |
| Hi iam lerning POSIX Threads for Redhat linux 9
i written one program, which displays current cursor position(it's
just like vi editor when we type some text in the editor it displays
the cursor position)
in this program iam getting some wrong output
i.e first 5 charectors of every line are not displaying on to the
screen
i don't know where is the problem ! help me plz...
The code was::
/* Progran to display row,clm position using Pthreads
Author: Subhash Reddy */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
void sig_handler(int );
static int i,get_sig=0;
char work_space[1024];
pthread_mutex_t work_mutex;
main ()
{
pthread_t thread1;
int res;
char ch;
void * thread1_res;
void * thread1_fun (void * );
void res_check (int, char * );
(void) signal(SIGINT, sig_handler);
system("clear");
//initscr();
res = pthread_mutex_init(&work_mutex,NULL);
res_check(res,"pthread_mutex_init");
res = pthread_create(&thread1, NULL, thread1_fun, NULL);
res_check(res,"pthread_create");
printf("\nEnter text :\n");
printf("Press Enter to start and ctrl+c to stop\n");
fflush(stdout);
while( i<=1000 && !get_sig)
{
ch = getchar();
putchar(ch);
pthread_mutex_lock(&work_mutex);
work_space[i++]=ch;
pthread_mutex_unlock(&work_mutex);
}
printf("\n Waiting for thread to finish. . .");
res = pthread_join(thread1, &thread1_res);
res_check(res,"pthread_join");
printf("\nThread returned : %s\n", (char *)thread1_res);
pthread_mutex_destroy(&work_mutex);
//endwin();
exit(EXIT_SUCCESS);
} // main
// Thread function start
void * thread1_fun(void * arg)
{
int j=0, xi=0, yi=0, x=0, y=0;
sleep(1);
initscr();
(void) signal(SIGINT, sig_handler);
while(!get_sig)
{
//getyx(stdscr, x, y);
//refresh();
pthread_mutex_lock(&work_mutex);
while(j<i)
{
getyx(stdscr, x, y);
if(y==108) { (y=-1,x++);}
if(x==27)
{
system("clear");
(x=0,y=-1);
move(28,93);
printw(" %2d,%2d ", yi, xi);
move(x,y+1);
continue;
}
if(work_space[j++]==13)
{
yi++;
xi=0;
x++;
y=-1;
}
else xi++;
move(28,93);
printw(" %2d,%2d ", yi, xi);
move(x,y+1);
refresh();
}
pthread_mutex_unlock(&work_mutex);
usleep(8);
}
endwin();
pthread_exit(" Thank you for using threads..\n");
} //End of thread1
//signal handler.
void sig_handler(int sig)
{
get_sig=1;
printf("\nSignal recived Press Enter to finish thread. . .\n");
(void) signal(SIGINT, SIG_DFL);
}
void res_check(int res, char * fun)
{
if(res)
{
printf("Function %s failed", fun);
exit(EXIT_FAILURE);
}
}
| |
| Nils O. Selåsdal 2006-08-22, 7:32 am |
| Subhash wrote:
> Hi iam lerning POSIX Threads for Redhat linux 9
>
> i written one program, which displays current cursor position(it's
> just like vi editor when we type some text in the editor it displays
> the cursor position)
> in this program iam getting some wrong output
> i.e first 5 charectors of every line are not displaying on to the
> screen
You're calling curses function from a thread, curses isn't
reentrant/thread safe.
You're also mixing curses and standard io functions which might
lead to a mess.
Calling printf from within a signal handler is not safe.
|
|
|
|
|