Sponsored Content
Full Discussion: Daemon process
Top Forums Programming Daemon process Post 302857293 by Don Cragun on Thursday 26th of September 2013 12:35:24 AM
Old 09-26-2013
Quote:
Originally Posted by sundaresh
I wish to make a process run in the background, but only one instance of it, and not many,
so when the program is loaded, it has to check whether another instance of the same
program is running and if so to exit. How do I do this ?
If your system supports the semget() and semop() calls that are included in one of the optional features in the POSIX standards, you can use semget() to get access to a semaphore with a key that is known to your daemon. After getting the semaphore ID you can use semop() with an array of two operations at the start of your program. These operations are:
Code:
sem_num   sem_op   sem_flg
=======   ======   =======
   0         0     IPC_NOWAIT
   0         1     SEM_UNDO

If the semop() call fails with EAGAIN, another daemon is already running and owns the semaphore. If the semop() call succeeds, this daemon owns the semaphore until it terminates. (It doesn't matter if this daemon terminates due to receipt of an uncaught signal or terminates normally by calling exit, the SEM_UNDO flag will release ownership of the semaphore when the daemon terminates.)

If you don't have access to a similar set of semaphore operations, a common approach is to atomically create (with open flags O_CREAT | O_EXCL) a lock file and write the PID of the daemon that created the lock into the lock file. The daemon should then remove the lock file before it exits. (If the daemon is killed before it can remove the lock file, another daemon won't be able to start until you manually remove the lock file.)
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

Daemon process

Hi, I have to write a daemon process, which performs certain operations in the background. Now since it performs operations in the background, it should not display anything to the standard output. The problem is that it still displays, text on standard output. Can anyone tell me (it is... (2 Replies)
Discussion started by: s_chordia
2 Replies

2. Programming

What is a daemon process?

This is gonna seem really silly to almost evryone here - but I need to know : what is a daemon process? Thanks (6 Replies)
Discussion started by: Kanu77
6 Replies

3. UNIX for Advanced & Expert Users

zombie daemon process!!

My daemon process is the child of init and init has the responsibility to remove it, once it turns zombie. But I want to ask why the daemon process which is child of init turns zombie in the first place. What measures I have to take to avoid this? rish (1 Reply)
Discussion started by: rish2005
1 Replies

4. Linux

daemon process

how i will write the daemon process,if any body have sample daemon process send me. (1 Reply)
Discussion started by: suresh_rupineni
1 Replies

5. Shell Programming and Scripting

How to starting process as daemon using ssh command?

Hello, I need to run a command on remote Linux using the ssh command from my local machine. I am able to execute the command on remote machine using ssh but it's behaving strangely. The command is supposed to start a daemon process on remote linux box and the control should return back to me... (5 Replies)
Discussion started by: nitinshukla
5 Replies

6. UNIX for Dummies Questions & Answers

How to write Pro*C daemon process using multithreading?

Hello, I am new to this forum and this is my first post here... I have never worked on either Pro*C or Multithreading..Now, i have to write a Pro*C, Multithreading daemon process.. I dont know where to start.. Can anybody help me with examples? 1. need to write a Pro*C multithreading... (0 Replies)
Discussion started by: kachiraju
0 Replies

7. Programming

How to find if a process a daemon ?

I have a scenario where I need to find if a process is a daemon process or not. This check needs to be done from within the process. I know there are no direct API's to do so. I have explored these options. 1. ctermid() - this can be unsuccessful as per the man pages 2. int devtty; if ((devtty... (7 Replies)
Discussion started by: vino
7 Replies

8. Shell Programming and Scripting

Diff between Bg and daemon process

Dear Unix Gurus, Plz provide major diff between background process and daemon process. Is it control available for daemon process?. (3 Replies)
Discussion started by: kkl
3 Replies

9. UNIX for Advanced & Expert Users

Needs help in launching a console application with the help of daemon process

Hi All, I am facing problem in launching a application with the help of a daemon process. Actually the application is based on command line that reads various commands for controlling the application from the console and accordingly executes those commands. The application always interact with... (3 Replies)
Discussion started by: gopallinux
3 Replies

10. Shell Programming and Scripting

run this script as a daemon process

Hi, HI , I have a simple script that moves files from one folder to another folder, I have already done the open-ssh server settings and the script is working fine and is able to transfer the files from one folder to another but right now I myself execute this script by using my creditianls to... (3 Replies)
Discussion started by: nks342
3 Replies
SEMOP(2)						     Linux Programmer's Manual							  SEMOP(2)

NAME
semop - semaphore operations SYNOPSIS
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semop(int semid, struct sembuf *sops, unsigned nsops); DESCRIPTION
A semaphore is represented by an anonymous structure including the following members: unsigned short semval; /* semaphore value */ unsigned short semzcnt; /* # waiting for zero */ unsigned short semncnt; /* # waiting for increase */ pid_t sempid; /* process that did last op */ The function semop performs operations on selected members of the semaphore set indicated by semid. Each of the nsops elements in the array pointed to by sops specifies an operation to be performed on a semaphore by a struct sembuf including the following members: unsigned short sem_num; /* semaphore number */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */ Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an operation asserts SEM_UNDO, it will be undone when the process exits. The set of operations contained in sops is performed atomically, that is, the operations are performed at the same time, and only if they can all be simultaneously performed. The behaviour of the system call if not all operations can be performed immediately depends on the presence of the IPC_NOWAIT flag in the individual sem_flg fields, as noted below. Each operation is performed on the sem_num-th semaphore of the semaphore set, where the first semaphore of the set is semaphore 0. There are three types of operation, distinguished by the value of sem_op. If sem_op is a positive integer, the operation adds this value to the semaphore value (semval). Furthermore, if SEM_UNDO is asserted for this operation, the system updates the process undo count (semadj) for this semaphore. This operation can always proceed - it never forces a process to wait. The calling process must have alter permission on the semaphore set. If sem_op is zero, the process must have read access permission on the semaphore set. This is a "wait-for-zero" operation: if semval is zero, the operation can immediately proceed. Otherwise, if IPC_NOWAIT is asserted in sem_flg, the system call fails with errno set to EAGAIN (and none of the operations in sops is performed). Otherwise semzcnt (the count of processes waiting until this semaphore's value becomes zero) is incremented by one and the process sleeps until one of the following occurs: o semval becomes 0, at which time the value of semzcnt is decremented. o The semaphore set is removed: the system call fails, with errno set to EIDRM. o The calling process catches a signal: the value of semzcnt is decremented and the system call fails, with errno set to EINTR. If sem_op is less than zero, the process must have alter permission on the semaphore set. If semval is greater than or equal to the abso- lute value of sem_op, the operation can proceed immediately: the absolute value of sem_op is subtracted from semval, and, if SEM_UNDO is asserted for this operation, the system updates the process undo count (semadj) for this semaphore. If the absolute value of sem_op is greater than semval, and IPC_NOWAIT is asserted in sem_flg, the system call fails, with errno set to EAGAIN (and none of the operations in sops is performed). Otherwise semncnt (the counter of processes waiting for this semaphore's value to increase) is incremented by one and the process sleeps until one of the following occurs: o semval becomes greater than or equal to the absolute value of sem_op, at which time the value of semncnt is decremented, the abso- lute value of sem_op is subtracted from semval and, if SEM_UNDO is asserted for this operation, the system updates the process undo count (semadj) for this semaphore. o The semaphore set is removed from the system: the system call fails with errno set to EIDRM. o The calling process catches a signal: the value of semncnt is decremented and the system call fails with errno set to EINTR. On successful completion, the sempid value for each semaphore specified in the array pointed to by sops is set to the process ID of the calling process. In addition, the sem_otime is set to the current time. RETURN VALUE
If successful the system call returns 0, otherwise it returns -1 with errno indicating the error. ERRORS
On failure, errno is set to one of the following: E2BIG The argument nsops is greater than SEMOPM, the maximum number of operations allowed per system call. EACCES The calling process has no access permissions on the semaphore set as required by one of the specified operations. EAGAIN An operation could not proceed immediately and IPC_NOWAIT was asserted in its sem_flg. EFAULT The address pointed to by sops isn't accessible. EFBIG For some operation the value of sem_num is less than 0 or greater than or equal to the number of semaphores in the set. EIDRM The semaphore set was removed. EINTR While blocked in this system call, the process caught a signal. EINVAL The semaphore set doesn't exist, or semid is less than zero, or nsops has a non-positive value. ENOMEM The sem_flg of some operation asserted SEM_UNDO and the system does not have enough memory to allocate the undo structure. ERANGE For some operation sem_op+semval is greater than SEMVMX, the implementation dependent maximum value for semval. NOTES
The sem_undo structures of a process aren't inherited across a fork(2) system call, but they are inherited across a execve(2) system call. semop is never automatically restarted after being interrupted by a signal handler, regardless of the setting of the SA_RESTART flags when establishing a signal handler. semadj is a per-process integer which is simply the (negative) count of all semaphore operations performed specifying the SEM_UNDO flag. When a semaphore's value is directly set using the SETVAL or SETALL request to semctl(2), the corresponding semadj values in all processes are cleared. The semval, sempid, semzcnt, and semnct values for a semaphore can all be retrieved using appropriate semctl(2) calls. The followings are limits on semaphore set resources affecting a semop call: SEMOPM Maximum number of operations allowed for one semop call (32). SEMVMX Maximum allowable value for semval: implementation dependent (32767). The implementation has no intrinsic limits for the adjust on exit maximum value (SEMAEM), the system wide maximum number of undo structures (SEMMNU) and the per-process maximum number of undo entries system parameters. BUGS
When a process terminates, its set of associated semadj structures is used to undo the effect of all of the semaphore operations it per- formed with the SEM_UNDO flag. This raises a difficulty: if one (or more) of these semaphore adjustments would result in an attempt to decrease a semaphore's value below zero, what should an implementation do? One possible approach would be to block until all the semaphore adjustments could be performed. This is however undesirable since it could force process termination to block for arbitrarily long peri- ods. Another possibility is that such semaphore adjustments could be ignored altogether (somewhat analogously to failing when IPC_NOWAIT is specified for a semaphore operation). Linux adopts a third approach: decreasing the semaphore value as far as possible (i.e., to zero) and allowing process termination to proceed immediately. CONFORMING TO
SVr4, SVID. SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC. SEE ALSO
ipc(5), semctl(2), semget(2), sigaction(2) Linux 2.4 2002-01-08 SEMOP(2)
All times are GMT -4. The time now is 09:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy