GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2004, 03:31
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road

Help on setsid() [Only for UNIX/Linux users]


Hi,

I'm a C newbie and doing my own shell prompt. I need a way to execute commands in a new seesion (window). I was told this could be done using setsid(). Here's my first attempt, the output should be a new window with date printed:

CPP / C++ / C Code:
 #include <sys/types.h>
 #include <unistd.h>
 #include <stdio.h>

 int main () {
 pid_t pid;

 printf("The process group ID is %d\n", (int) getpgrp());

 if ((pid = setsid()) == -1)
    perror("setsid error");
 else {
    printf("The new process group ID is %d\n", (int) pid);
    execl("/bin/date", "date", NULL, NULL);
 }
 return 0;
 }


Apparently, I got an error in UNIX : Not Owner
1) Is there anthing that I did wrong ?
2) Is there any other way that I can exec commands in a new window?

Thanks.
  #2  
Old 03-Apr-2004, 08:56
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi nusstu.

Maybe you can clarify exactly what you mean by "exec commands in a new window" Do you mean that you want to pull up a new terminal window?

I really don't think that setsid is going to do you a whole lot of good. From my limited understanding it will fail every time in the function that you have written, because your function is the "group leader" and so the PID that is being attempted to be returned is the PID of your function. This results in an error.

Here is a quote from the man page:
Quote:
setsid() creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session, the process group leader of the new process group, and has no controlling tty. The process group ID and session ID of the calling process are set to the PID of the calling process. The calling process will be the only process in this new process group and in this new session.

The key portion is "if the calling process is not a process group leader".

Here is a bash script that I use to spawn a terminal session (using konsole) that you could call using the system command:
Code:
export DISPLAY=localhost:0.0 exec konsole -caption 'Raven Konsole' -ls &

This will draw a seperate terminal window with the title set to Raven Konsole. Not sure if this helps or not, but if you give us more of an idea of what it is you want to accomplish, we may be able to help you out more.

Good Luck!
d
  #3  
Old 03-Apr-2004, 09:48
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road
Hi dsmith,

Yes I wanna pull up a new window, what i wanna do is simply run the /bin/date command in a new window from a C file. So the 1st window runs main and the 2nd displays date. Is that possible to do in C? I think ur script file isn't what i'm looking for I want to open a new window from a C file.

"The calling process is the leader of the new session, the process group leader of the new process group, and has no controlling tty"

What's a process group leader, and what does controlling tty mean?

Thanks.
  #4  
Old 03-Apr-2004, 10:21
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
It sounds like you want to be able to run these concurrently? This is really simple if you want the parent process to wait for the child process to complete (system()), but what you want is probably more like (pthread_create()). I don't have alot of experience here, but if you have access to the man pages, pull up the man page for pthread_create.
Quote:
SYNOPSIS
#include <pthread.h>

int pthread_create(pthread_t * thread, pthread_attr_t *
attr, void * (*start_routine)(void *), void * arg);

DESCRIPTION
!pthread_create! creates a new thread of control that exe-
cutes concurrently with the calling thread. The new thread
applies the function |start_routine| passing it |arg| as
first argument. The new thread terminates either explic-
itly, by calling !pthread_exit!(3), or implicitly, by
returning from the |start_routine| function. The latter
case is equivalent to calling !pthread_exit!(3) with the
result returned by |start_routine| as exit code.

The problem with the exec family of processes is that it replaces your current process:
Quote:
execve() does not return on success, and the text, data,
bss, and stack of the calling process are overwritten by
that of the program loaded.

I don't think that is what you want either. The only thing I found that came close to doing what your are taking about is pthread_create, which may or may not be linux specific.

Sorry, I can't be of more help.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.