-2

So i have this program that is supposed to be a (super basic) server in Linux that shows me this menu:

What u want to do?
 0 -> Exit
 1 -> Add product
 2 -> Show products list
 3 -> Turn on server

And i want it so that when you put the number 3, the server.c stops, like when you press CTRL + Z and send the commnad "bg" so that the server runs in backgroundg (like when you do the command "./server&") so i can open the client.c and interact with the server.

Example of what i want it to do when you input 3 (on here i did it manually on the propmt):

estgv18708@viriato:~/tp6$ ./server

What u want to do?
 0 -> Exit
 1 -> Add product
 2 -> Show products list
 3 -> Turn on server
3

^Z
[1]+  Stopped                 ./server
estgv18708@viriato:~/tp6$ bg
[1]+ ./servidor &

Thanks in advance! :D

Enguias
  • 19
  • 2
  • 1
    A "stopped" or suspended program is not the same as a program running in the background. To suspend a program send SIGSTOP (or control+Z). You can send signals to a pid using the `kill` function in `signal.h` – h0r53 Jan 08 '21 at 18:45
  • I think what you want is to create a daemon, see https://stackoverflow.com/questions/17954432/creating-a-daemon-in-linux – Geoffroy Jan 08 '21 at 18:49
  • It sounds like what you really want is the server program to run independently so you can run your server and client at the same time. The best way to do this is to open two terminals so each session has its own TTY. Otherwise your IO will be scrambled between the two. It seems like you've also confused suspended programs with programs running in the background. Background programs are essentially forked and executing within the same TTY, while suspended programs are truly stopped from executing until they receive a signal to resume. – h0r53 Jan 08 '21 at 18:50
  • As @Geoffroy mentioned, a production use of your server should be configured via a daemon, but you'll lose IO in your daemon and thus a menu isn't really a great idea. – h0r53 Jan 08 '21 at 18:52
  • 1
    You can still run the menu and then only fork into a daemon no? @h0r53 – Geoffroy Jan 09 '21 at 07:11

2 Answers2

1

Probably an XY-problem here -- why would you want to do this?

The basic issue is that this presupposes your server program is run from a shell and you want to return to the shell to do stuff. But what is the server going to be doing in the meantime? The shell will have (need) control of the input and output so the server pretty much just needs to wait.

You can get the same effect as ^z by calling kill:

kill(getpid(), SIGSTOP);

will have the server send a STOP signal to itself. The shell (which is monitoring progress of the program), will notice this and print the "Stopped" message and then prompt for what to do. The server will be stopped, and you can resume it with the shell's fg command (or put it into the background with bg). However, if your server program is not run from a shell (it is just running directly in a terminal), this will simply stop the server and nothing else will happen.

More commonly what you want to do here is run a subshell:

system(getenv("SHELL"));

which will start a new shell and will wait for it to complete. When the shell exits (use the exit command or type ^d at the shell prompt) the server will resume.

If you want the server to continue to run while the shell runs, you can use fork+exec to run the shell in the child and continue running the server in the parent, but you need to be careful about using the terminal input and output (the server and shell will be fighting for it).

Chris Dodd
  • 101,438
  • 11
  • 111
  • 197
-2

Cant u just do system("bg"); ? U dond need to stop the program just need to put it on the background

  • no, i tried it and it didn't work, but i want something simillar because i don't want to actually stop server.c, i want to make it run in background by itself after the input "3" is given to the program. – Enguias Jan 08 '21 at 19:13
  • does anyone know how to do it? – 1002 1002 Jan 08 '21 at 19:17