Sponsored Content
Top Forums Programming Multi process programming in C Post 302894454 by alister on Tuesday 25th of March 2014 02:08:48 PM
Old 03-25-2014
As before, I did not compile or test the code. There may be other issues.

content is allocated and freed but never used.

fd will never contain an error code; it's always a pointer value. When that pointer is NULL, it indicates an error and the error code is in errno. That's what you should be inspecting during error handling. To convert the integer errno into a useful message, something like strerror() will help.

As you discovered, inserting arbitrary text into a script can present serious issues. The simplest solution is to write the reminder's text to a separate file.

strcpy of optarg to text is unsafe and can overflow. You can use strncpy, but then must be careful to ensure that the string is always null-terminated. strlcpy is a simpler alternative, if available. However, the simplest alternative in this case is to not copy at all.

Not only do you not need to copy, you don't need to allocate either. The reminder's text has already been allocated storage during startup. It's in argv. The only thing you need to do is pass around the pointer. The default value has also been stored away in the executable's image and it's location can also be passed around.

You implied that you are no longer waiting in the parent, but since the code is still there, a couple of notes about it.

You should always check WIFEXITED before using WEXITSTATUS (as you did with WIFSIGNALED before using WTERMSIG). An implementation is not forbidden from overloading bits for signal and status information (though I don't know if any implementation actually does so).

For portability and readability (especially for readability), it's a far better choice to use the macros in signal.h. Instead of 6, use SIGABRT. As far as I know, even though the kill(1) utility is required to recognize -6 as SIGABRT, nothing requires the kill(2) system call implementation to equate 6 with SIGABRT (although it almost certainly does).

Regarding your interprocess communication question, your pid file idea seems perfectly reasonable.

Regards,
Alister
This User Gave Thanks to alister For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell programming for process?

If I want to write program that spread the work to its child process so that each process compute some task what should I do? The objective of my program is to fill in the table which (10*10) in dimension and each column is filled with the fibonacci value of i+j (i mean current row and j mean... (1 Reply)
Discussion started by: robocup
1 Replies

2. UNIX for Dummies Questions & Answers

Multi User Multi Task

Dear Experts Why we always hear that unix operating system is Multi User and Multi task. What does these two means. I have looked at some books and documents but couldn't find aclear explenation. Can we say Windows operating system is also multi user and multi task?? Thanks for your help in... (6 Replies)
Discussion started by: Reza Nazarian
6 Replies

3. Programming

message queues and multi-process

Hi, Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer... (10 Replies)
Discussion started by: rvan
10 Replies

4. Programming

Redirect Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); printf("Hello \n");... (3 Replies)
Discussion started by: djodjo
3 Replies

5. Programming

Redirect Standard Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); ... (5 Replies)
Discussion started by: djodjo
5 Replies

6. High Performance Computing

What is it about OpenMosix that supports multi-process applications?

I read that 'Any single program that can run as multiple processes can benefit from OpenMosix: "The GIMP" photo editor and the "kandel" fractal generator are known to do this. Are there other load-balancing clusters that do support multi-process applications? (1 Reply)
Discussion started by: Advice Pro
1 Replies

7. Shell Programming and Scripting

Multi thread shell programming

I have a unix directory where a million of small text files getting accumulated every week. As of now there is a shell batch program in place which merges all the files in this directory into a single file and ftp to other system. Previously the volume of the files would be around 1 lakh... (2 Replies)
Discussion started by: vk39221
2 Replies

8. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

9. Programming

Multi head/multi window hello world

I am trying to write a large X app. I have successfully modified my xorg.conf to setup 4 monitors on an NVIDIA Quatro5200. I am trying to modify a simple hello world application to open a window on three of the four monitors. depending on the changes to loop the window creation section and event... (2 Replies)
Discussion started by: advorak
2 Replies

10. Shell Programming and Scripting

Use the get and post method in the bash ( multi process)?

hi I want to call a lot of links with the post method What to do to speed it up?? ####This method is slow #!/bin/bash func2() { index1=0 while read line ; do index1=$(($index1+1)) url=$line done < tmp/url1.txt } (10 Replies)
Discussion started by: mnnn
10 Replies
STRCPY(3)						     Linux Programmer's Manual							 STRCPY(3)

NAME
strcpy, strncpy - copy a string SYNOPSIS
#include <string.h> char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); DESCRIPTION
The strcpy() function copies the string pointed to by src, including the terminating null byte (''), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns! (See BUGS.) The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated. If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written. A simple implementation of strncpy() might be: char * strncpy(char *dest, const char *src, size_t n) { size_t i; for (i = 0; i < n && src[i] != ''; i++) dest[i] = src[i]; for ( ; i < n; i++) dest[i] = ''; return dest; } RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +--------------------+---------------+---------+ |Interface | Attribute | Value | +--------------------+---------------+---------+ |strcpy(), strncpy() | Thread safety | MT-Safe | +--------------------+---------------+---------+ CONFORMING TO
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. NOTES
Some programmers consider strncpy() to be inefficient and error prone. If the programmer knows (i.e., includes code to test!) that the size of dest is greater than the length of src, then strcpy() can be used. One valid (and intended) use of strncpy() is to copy a C string to a fixed-length buffer while ensuring both that the buffer is not over- flowed and that unused bytes in the target buffer are zeroed out (perhaps to prevent information leaks if the buffer is to be written to media or transmitted to another process via an interprocess communication technique). If there is no terminating null byte in the first n bytes of src, strncpy() produces an unterminated string in dest. If buf has length buflen, you can force termination using something like the following: strncpy(buf, str, buflen - 1); if (buflen > 0) buf[buflen - 1]= ''; (Of course, the above technique ignores the fact that, if src contains more than buflen - 1 bytes, information is lost in the copying to dest.) strlcpy() Some systems (the BSDs, Solaris, and others) provide the following function: size_t strlcpy(char *dest, const char *src, size_t size); This function is similar to strncpy(), but it copies at most size-1 bytes to dest, always adds a terminating null byte, and does not pad the target with (further) null bytes. This function fixes some of the problems of strcpy() and strncpy(), but the caller must still handle the possibility of data loss if size is too small. The return value of the function is the length of src, which allows truncation to be easily detected: if the return value is greater than or equal to size, truncation occurred. If loss of data matters, the caller must either check the arguments before the call, or test the function return value. strlcpy() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library. BUGS
If the destination string of a strcpy() is not large enough, then anything might happen. Overflowing fixed-length string buffers is a favorite cracker technique for taking complete control of the machine. Any time a program reads or copies data into a buffer, the program first needs to check that there's enough space. This may be unnecessary if you can show that overflow is impossible, but be careful: pro- grams can get changed over time, in ways that may make the impossible possible. SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), stpcpy(3), stpncpy(3), strdup(3), string(3), wcscpy(3), wcsncpy(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2017-09-15 STRCPY(3)
All times are GMT -4. The time now is 01:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy