Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting started with Background C or Capp
#1
Hello,

I'm trying to get up and running with my first C app using two very simple threads. I got the code to compile, but I'm not sure how to go about starting execution. I tried using the task manager, but there was no indication that the task was running. I also had to put casts on the "Command" function call because I was getting a warning regarding deprecation from Constant Char * to char *. I'm not sure if this has anything to do with my problem, just thought I'd mention it. Any advice on creating a simple multithreaded program and executing it would be helpful. I am also unsure of what priority to assign the threads.

#include <gplib.h>
#include <RtGpShm.h> // Global Rt/Gp Shared memory pointers
//-------------------------------------------------------------
// The following is a projpp created file from the User defines
//-------------------------------------------------------------
#include "../../Include/pp_proj.h"

void * thread1code(void * args)
{
while(1)
{
if(GetGlobalVar(TrigJog) == 1)
{
Command((char *)"#1j/");
while(GetGlobalVar(TrigJog) == 1)
{
Command((char *)"#1j+");
sleep(1);
Command((char *)"#1j-");
sleep(1);
}
Command((char *)"#1k");
}
sleep(1);
}
}

void * thread2code(void * args)
{
while(1)
{
if(GetGlobalVar(TrigJog) == 1)
{
Command((char *)"#2j/");
while(GetGlobalVar(TrigJog) == 1)
{
Command((char *)"#2j+");
sleep(1);
Command((char *)"#2j-");
sleep(1);
}
Command((char *)"#2k");
}
sleep(1);
}
}

int main(void)
{

pthread_t thread1, thread2;
pthread_attr_t attr1, attr2;
struct sched_param sp1, sp2;
sp1.sched_priority = 1;
sp2.sched_priority= 99;

pthread_attr_init(&attr1);
pthread_attr_init(&attr2);

pthread_attr_setschedpolicy(&attr1, SCHED_FIFO);
pthread_attr_setschedpolicy(&attr2, SCHED_FIFO);

pthread_attr_setschedparam(&attr1, &sp1);
pthread_attr_setschedparam(&attr2, &sp2);

pthread_create(&thread1, &attr1, thread1code, 0);
pthread_create(&thread2, &attr2, thread2code, 0);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

return 0;
}
Reply
#2
1. The warning is just from the C++ compiler option we enable to give maximum feedback.

2. Thanks, you found a hole in the task manager. Your program is crashing and we are not showing that. It is now in the wish list.

As to why the program is crashing I do not know. Since we do not have a debugger yet you will need to use printf() to find it.

Open a telnet session to the ppmac and go to
root@10.34.9.223://var/ftp/usrflash/Project/C Language/Background Programs#

Then type ./capp1.out

And you get

Segmentation fault
root@10.34.9.223://var/ftp/usrflash/Project/C Language/Background Programs#

So you will need to either use telnet and printf() to get some clues or the IDE unsolicited response window and the SEND command
sprintf(szdata,"%d\n", pshm->P[4]);
Send(SEND1_PORT,szdata);

Just a reminder I am not here on Fridays.
Reply
#3
Thank you,

Now that I know we are just executing that program name it will allow me to debug more effectively. The Send() seems to be a powerful tool as well. I'll also take the time today to process all the return values and use that with the printf/send statements to try and see what is going on.

It is nice to have the software wish list. For now the Telnet session is more useful, however.

~Ken

(11-19-2009, 04:59 PM)bradp Wrote: 1. The warning is just from the C++ compiler option we enable to give maximum feedback.

2. Thanks, you found a hole in the task manager. Your program is crashing and we are not showing that. It is now in the wish list.

As to why the program is crashing I do not know. Since we do not have a debugger yet you will need to use printf() to find it.

Open a telnet session to the ppmac and go to
root@10.34.9.223://var/ftp/usrflash/Project/C Language/Background Programs#

Then type ./capp1.out

And you get

Segmentation fault
root@10.34.9.223://var/ftp/usrflash/Project/C Language/Background Programs#

So you will need to either use telnet and printf() to get some clues or the IDE unsolicited response window and the SEND command
sprintf(szdata,"%d\n", pshm->P[4]);
Send(SEND1_PORT,szdata);

Just a reminder I am not here on Fridays.
Reply
#4
I managed to get my app running on Friday. I have it running in FIFO periodic mode with a 1ms "wakeup" time. I'm not sure if this was the original issue or not, but the "Command()" function call seems to crash the program with the message "Segmentation Fault". I'm going to look at increasing the stack size for my threads so that I can call the DT functions (stack size is at the xenomai default now).
I changed my stacksize up to 64k and still was getting a seg fault. I ran GDB on it and got the following message when it had the errror. Note that "line 56" contains "Command("#1 j/");". I also attached the code so you can see what I'm doing. The code works well as long as I comment out the calls to "Command()" function.

*** GDB output ***

Starting program: /var/ftp/usrflash/Project/C Language/Background Progr
.out
[Thread debugging using libthread_db enabled]
[New Thread 0x30026770 (LWP 11228)]
[New Thread 0x308304d0 (LWP 11232)]
[Switching to Thread 0x308304d0 (LWP 11232)]

Breakpoint 1, thread1code (args=0x0) at capp1.c:53
53 capp1.c: No such file or directory.
in capp1.c
(gdb) next
56 in capp1.c
(gdb) next

Program received signal SIGSEGV, Segmentation fault.
0x0fe4df1c in PPCmdProcessor () from /opt/ppmac/libppmac/libppmac.so

*** End GDB output ***

*** Begin code ***
//--------------------------------------------------------------------------------
#include <gplib.h>
#include <RtGpShm.h> // Global Rt/Gp Shared memory pointers

//-------------------------------------------------------------
// The following is a projpp created file from the User defines
//-------------------------------------------------------------
#include "../../Include/pp_proj.h"

static const int ThreadRetOK = 1;
static const int ThreadRetFail = -1;
long int thread1time, thread2time;

void create_thread_attr_rt(pthread_attr_t * attr)
{
pthread_attr_init(attr); //Create attributes with defaults
// pthread_attr_setstacksize(attr, PTHREAD_STACK_MIN*10);
pthread_attr_setstacksize(attr, 65536);
pthread_attr_setschedpolicy(attr, SCHED_FIFO);
}


void make_thread_periodic(int TaskNumber)
{
struct timespec start;
struct timespec period;
int retval;

start.tv_sec = 0;
start.tv_nsec = 0;

period.tv_sec = 0;
period.tv_nsec = 1000000;

if(retval = pthread_make_periodic_np (pthread_self(), &start, &period))
{
printf("make periodic Thread %d failed. ret: %d, ESRCH: %d, ETIMEDOUT: %d\n",TaskNumber, retval, ESRCH, ETIMEDOUT);
}
}

long int get_time_ns(void)
{
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return time.tv_nsec;
}

void * thread1code(void * args)
{
long int StartMeasure, Elapsed;
int i;

make_thread_periodic(1);

//printf("Got before command\n");
Command("#1 j/");
//printf("after command\n");

StartMeasure = get_time_ns();
for(i=0; i<5000; i++)
{
pthread_wait_np(NULL);
}
Elapsed = get_time_ns() - StartMeasure;

thread1time = Elapsed;

//Command("#1 k");

return ((void *) &ThreadRetOK);
}

void * thread2code(void * args)
{
long int StartMeasure, Elapsed;
int i;

make_thread_periodic(2);

StartMeasure = get_time_ns();
for(i=0; i<60; i++)
{
pthread_wait_np(NULL);
}
Elapsed = get_time_ns() - StartMeasure;

thread2time = Elapsed;

return ((void *) &ThreadRetOK);
}

int main(void)
{
struct timespec start;
struct timespec period;
int retval;
pthread_t thread1, thread2;
pthread_attr_t rt_thread_attr;

/* Avoids memory swapping for this program */
mlockall(MCL_CURRENT|MCL_FUTURE);

start.tv_sec = 0;
start.tv_nsec = 0;

period.tv_sec = 0;
period.tv_nsec = 1000000;

create_thread_attr_rt(&rt_thread_attr);

//retval = pthread_create(&thread1, NULL, thread1code, 0);
retval = pthread_create(&thread1, &rt_thread_attr, thread1code, 0);
if(retval != 0) printf("Thread1 return: %d\n", retval);

//retval = pthread_create(&thread2, NULL, thread2code, 0);
retval = pthread_create(&thread2, &rt_thread_attr, thread2code, 0);
if(retval != 0) printf("Thread2 return: %d\n", retval);

//if(retval = pthread_make_periodic_np (thread1, &start, &period))
//{
// printf("make periodic Thread 1 failed. ret: %d, ESRCH: %d, ETIMEDOUT: %d\n", retval, ESRCH, ETIMEDOUT);
//}

//if(retval = pthread_make_periodic_np (thread2, &start, &period))
//{
// printf("make periodic Thread 2 failed. ret: %d, ESRCH: %d, ETIMEDOUT: %d\n", retval, ESRCH, ETIMEDOUT);
//}

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

printf("Thread1 time: %ld (ms)\n", thread1time/1000000);
printf("Thread2 time: %ld (ms)\n", thread2time/1000000);

return 0;
}


*** End Code ***
Reply
#5
My first guess would be that the API library is not initialized. I do not see any calls to InitLibrary() or CloseLibrary(). This would mean that a library call in the Main() would also crash.

Next question is if the library transfers to the threads. What I read makes me think it does but I am not sure and this week have nobody to ask. So once you get the API call working in the main() then try it in a thread to see if the library can be used by a thread.
Reply
#6
It *was* the lack of library initialization that caused the seg fault. This was in the program example but I had overlooked it. I put it in and now the DT "Command()" works from either the threads, or the original process "main()".

I think at this point we are dangerous, and I look forward to seeing what else we can do with this system.

Thank you,
~Ken
Reply
#7
Hello,

This is my first post to this forum, so please bear with me.
I am using a Power PMAC firmware version 2.1.1.3 and IDE version 2.2.0.39.
I am trying to use printf() and send() to debug a Background C application. The code below does not produce any output at the IDE Terminal Window or at the Unsolicited Window, even though I have enabled Buffers 0 through 7 and set the Control Properties to ActivateSendPort 0. Any help will be appreciated.

Nadir



#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
int sendport;
sendport = 0;
Send(sendport,"Hello\n");
printf("Hello\n");
CloseLibrary();
return 0;
}
Reply
#8
Make sure the appropriate bits of Sys.SendFileMode are set to “1”. This is required by the “Unsolicited Messages” window in the IDE.
Reply
#9
(08-20-2018, 11:57 AM)NadirN Wrote: Hello,

This is my first post to this forum, so please bear with me.
I am using a Power PMAC firmware version 2.1.1.3 and IDE version 2.2.0.39.
I am trying to use printf() and send() to debug a Background C application. The code below does not produce any output at the IDE Terminal Window or at the Unsolicited Window, even though I have enabled Buffers 0 through 7 and set the Control Properties to ActivateSendPort 0. Any help will be appreciated.

Nadir



#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
int sendport;
sendport = 0;
Send(sendport,"Hello\n");
printf("Hello\n");
CloseLibrary();
return 0;
}

The IDE terminal is not a Linux terminal. Use Putty to setup a Linux SSH terminal for your printf statements. Also, building a serial cable and running putty to the serial connection is helpful as this is the Linux error device.
Reply
#10
(08-21-2018, 05:10 AM)JeffLowe Wrote:
(08-20-2018, 11:57 AM)NadirN Wrote: Hello,

This is my first post to this forum, so please bear with me.
I am using a Power PMAC firmware version 2.1.1.3 and IDE version 2.2.0.39.
I am trying to use printf() and send() to debug a Background C application. The code below does not produce any output at the IDE Terminal Window or at the Unsolicited Window, even though I have enabled Buffers 0 through 7 and set the Control Properties to ActivateSendPort 0. Any help will be appreciated.

Nadir



#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
int sendport;
sendport = 0;
Send(sendport,"Hello\n");
printf("Hello\n");
CloseLibrary();
return 0;
}

The IDE terminal is not a Linux terminal. Use Putty to setup a Linux SSH terminal for your printf statements. Also, building a serial cable and running putty to the serial connection is helpful as this is the Linux error device.

Hi Jeff,
Thank you very much for replying.
I downloaded Putty and logged into Linux. I ran the compiled C app "myprint.out"(code shown below.)

#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
InitLibrary();
printf("Hello\n");
CloseLibrary();
return 0;
}

The Linux command line is:
root@192.168.0.232://# /var/ftp/usrflash/Project/C\ Language/Background\ Programs/myprint.out

The Linux response is:
Segmentation fault
root@192.168.0.232:/var#

Any ideas?

Nadir
Reply
#11
(08-22-2018, 11:49 AM)NadirN Wrote:
(08-21-2018, 05:10 AM)JeffLowe Wrote:
(08-20-2018, 11:57 AM)NadirN Wrote: Hello,

This is my first post to this forum, so please bear with me.
I am using a Power PMAC firmware version 2.1.1.3 and IDE version 2.2.0.39.
I am trying to use printf() and send() to debug a Background C application. The code below does not produce any output at the IDE Terminal Window or at the Unsolicited Window, even though I have enabled Buffers 0 through 7 and set the Control Properties to ActivateSendPort 0. Any help will be appreciated.

Nadir



#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
int sendport;
sendport = 0;
Send(sendport,"Hello\n");
printf("Hello\n");
CloseLibrary();
return 0;
}

The IDE terminal is not a Linux terminal. Use Putty to setup a Linux SSH terminal for your printf statements. Also, building a serial cable and running putty to the serial connection is helpful as this is the Linux error device.

Hi Jeff,
Thank you very much for replying.
I downloaded Putty and logged into Linux. I ran the compiled C app "myprint.out"(code shown below.)

#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
InitLibrary();
printf("Hello\n");
CloseLibrary();
return 0;
}

The Linux command line is:
root@192.168.0.232://# /var/ftp/usrflash/Project/C\ Language/Background\ Programs/myprint.out

The Linux response is:
Segmentation fault
root@192.168.0.232:/var#

Any ideas?

Nadir

InitLibrary(); // missing?
Reply
#12
(08-22-2018, 02:01 PM)JeffLowe Wrote:
(08-22-2018, 11:49 AM)NadirN Wrote:
(08-21-2018, 05:10 AM)JeffLowe Wrote:
(08-20-2018, 11:57 AM)NadirN Wrote: Hello,

This is my first post to this forum, so please bear with me.
I am using a Power PMAC firmware version 2.1.1.3 and IDE version 2.2.0.39.
I am trying to use printf() and send() to debug a Background C application. The code below does not produce any output at the IDE Terminal Window or at the Unsolicited Window, even though I have enabled Buffers 0 through 7 and set the Control Properties to ActivateSendPort 0. Any help will be appreciated.

Nadir



#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
int sendport;
sendport = 0;
Send(sendport,"Hello\n");
printf("Hello\n");
CloseLibrary();
return 0;
}

The IDE terminal is not a Linux terminal. Use Putty to setup a Linux SSH terminal for your printf statements. Also, building a serial cable and running putty to the serial connection is helpful as this is the Linux error device.

Hi Jeff,
Thank you very much for replying.
I downloaded Putty and logged into Linux. I ran the compiled C app "myprint.out"(code shown below.)

#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
InitLibrary();
printf("Hello\n");
CloseLibrary();
return 0;
}

The Linux command line is:
root@192.168.0.232://# /var/ftp/usrflash/Project/C\ Language/Background\ Programs/myprint.out

The Linux response is:
Segmentation fault
root@192.168.0.232:/var#

Any ideas?

Nadir

InitLibrary(); // missing?

That command was already in the code.
I made the following change to make sure the library call returns OK.

#include <gplib.h> // Global Gp Shared memory pointer
#include "../../Include/pp_proj.h"
int main(void)
{
if (InitLibrary() ==0)
{
printf("Hello\n");
CloseLibrary();
}
return 0;
}

First I got the expected response "Hello". Yay !
But after a few runs, edits, undoing edits and going back to the same code I got
"Segmentation fault" again. No run time errors.
Something is changing between runs. Any ideas?


Thanks,
Nadir
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)