Unix Technical Forum

can u plz check this program and give some Suggestions

This is a discussion on can u plz check this program and give some Suggestions within the comp.unix.solaris forums, part of the Solaris Operating System category; --> Hi iam lerning POSIX Threads for Redhat linux 9 i written one program, which displays current cursor position(it's just ...


Go Back   Unix Technical Forum > Unix Operating Systems > Solaris Operating System > comp.unix.solaris

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-11-2008, 05:53 PM
Subhash
 
Posts: n/a
Default can u plz check this program and give some Suggestions


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);
}
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:40 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com