Sponsored Content
Homework and Emergencies Homework & Coursework Questions Please help with the following fork code..with complete explanation Post 302362915 by prakashabii on Sunday 18th of October 2009 05:44:47 PM
Old 10-18-2009
Java Please help with the following fork code..with complete explanation

I have the following piece of codes. Please explain it to me in great detail how are these codes working.

1. #include <stdio.h>
int main(){
int x;
x=0;
while (x<2 && fork()){
if (!fork()) execlp("echo","x++","x",0);
x++;
system("echo x+x");
}
}

2.

#include <stdio.h>
int i;
int main() {
for(i=0;i<2;i++){
if(fork())
fork();
}
printf ("%d\n", i);
system(("echo i++");
}


The attempts at a solution (include all code and scripts):[/B]
Output of 1:
x
x+x
x+x
x


Output of 2:
2
i++
2
i++
2
i++
2
i++
2
i++
2
i++
2
i++
2
2
i++
i++




Politechnico di Torino

Last edited by prakashabii; 10-18-2009 at 06:51 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need explanation for the syntax(code)

Hi I am new to shell script programming... want to know the process of the following: if then echo "$0: missing argument for option(s) :$MISSINGOPTARG" echo "usage" $USAGE" exit 1 fi (1 Reply)
Discussion started by: chandhar
1 Replies

2. Shell Programming and Scripting

AWK deifnition and some small code explanation

Could someone give me a quick simple explanation for the AWK command. And also help me to explain the code i have made. I have made some general comments about it myself. I was wondering if people could help me with the rest: awk -F'' 'END { fmt = "%-20s\t%s\t%s\n" ... (0 Replies)
Discussion started by: amatuer_lee_3
0 Replies

3. Shell Programming and Scripting

nawk code explanation

Hi I have a line/command which greps certain pattern () from the file and 22 lines AFTER that: nawk '/\/{c=22}c&&c--' input_file Can somebody explain the coding in this command (what " c&&c-- " does)? and how to modify this command to output 22 lines BEFORE this pattern () Thanks a lot ... (3 Replies)
Discussion started by: aoussenko
3 Replies

4. Programming

Help me complete my code.

Ok, so I have done the 'mathematical part' just I am not sure where to put them in and how to get them to return answers. public class Main { public static void main(String args) { } //Part A. //1&2- Computes the sum andproduct of all of the elements of the array a. ... (5 Replies)
Discussion started by: HardyV2
5 Replies

5. Shell Programming and Scripting

code explanation

Can you please explain the following code plz? my_cd=' ' while getopts :e: OPTION; do case "$OPTION" in e) my_cd ="$OPTARG";; esac done if ; then echo " >>> ERROR - I am wrong" echo " >>> ERROR - Hello" exit 99 fi What I don't understand is what is OPTION or... (3 Replies)
Discussion started by: RubinPat
3 Replies

6. UNIX for Dummies Questions & Answers

Explanation of NAWK Code

Hi all, I need your help in understanding the meaning and syntax of the below nawk line. Here is an extract of a script which I use daily and works well. The script extracts the hostnames and messages within a syslog file. I would also like to extract the message time in the 3rd column by... (2 Replies)
Discussion started by: wthomas
2 Replies

7. UNIX for Dummies Questions & Answers

Code explanation?

I need someone to tell me how exactly works the following code αfter /etc/passwd :eek:: cat /etc/passwd|grep "^:"|sed '1,$s/^\(*\):*:\(*\):.*$/ \1 \2 /'|sort -nrk3 -t:I want a good explanation to understand the code please (4 Replies)
Discussion started by: kotsos13
4 Replies

8. Homework & Coursework Questions

Minix 3 Source Code Organisation Explanation

1. I've been asked to provide a detailed description on how Minix's source code is organised. This may sound slightly simple, but what exactly am I to refer to when explaining? Any documents that would provide assistance to understand whats being asked? Thanks 2. Not applicable: 3.... (1 Reply)
Discussion started by: Menace12
1 Replies

9. Shell Programming and Scripting

Need explanation on Perl Fork

Hi, I am breaking my head to understand the below line of code in perl: next if $pid = fork; # Parent goes to next server. die "fork failed: $!" unless defined $pid; Can anyone please explain me in detail as I am a slow learner? Thanks, Ali. (2 Replies)
Discussion started by: liyakathali
2 Replies

10. Shell Programming and Scripting

ksh code explanation

Hi. Can somebody please explain the following lines of KSH code for me? The code checks all sub directories in a specific location which are numbered (E.g. test_01, test_02 ... etc.), then finds the one with highest number and extracts that number from the dir name into the variable num. I'd just... (9 Replies)
Discussion started by: user052009
9 Replies
LIBPFM(3)						     Linux Programmer's Manual							 LIBPFM(3)

NAME
pfm_get_event_encoding - get raw event encoding SYNOPSIS
#include <perfmon/pfmlib.h> int pfm_get_event_encoding(const char *str,int dfl_plm, char **fstr, int *idx, uint64_t *code, int *count); DESCRIPTION
This function is used to retrieve the raw event encoding corresponding to the event string in str. The string may contain unit masks and modifiers. The default privilege level mask is passed in dfl_plm. It may be used depending on the event. This function is deprecated. It is superseded by pfm_get_os_event_encoding() where the OS is set to PFM_OS_NONE. Encoding is retrieve through the pfm_pmu_encode_arg_t structure. The following examples illustrates the transition: int i, count = 0; uint64_t *codes; ret = pfm_get_event_encoding("RETIRED_INSTRUCTIONS", PFM_PLM3, NULL, &codes, &count); if (ret != PFM_SUCCESS) err(1", cannot get encoding %s", pfm_strerror(ret)); for(i=0; i < count; i++) printf("count[%d]=0x%"PRIx64" ", i, codes[i]); is equivalent to: pfm_pmu_encode_arg_t arg; int i; memset(&arg, 0, sizeof(arg)); arg.size = sizeof(arg); ret = pfm_get_os_event_encoding("RETIRED_INSTRUCTIONS", PFM_PLM3, PFM_OS_NONE, &arg); if (ret != PFM_SUCCESS) err(1", cannot get encoding %s", pfm_strerror(ret)); for(i=0; i < arg.count; i++) printf("count[%d]=0x%"PRIx64" ", i, arg.codes[i]); free(arg.codes); The encoding may take several 64-bit integers. The function can use the array passed in code if the number of entries passed in count is big enough. However, if both *codes is NULL and count is 0, the function allocates the memory necessary to store the encoding. It is up to the caller to eventually free the memory. The number of 64-bit entries in codes is reflected in *count upon return regardless of whether the codes was allocated or used as is. If the number of 64-bit integers is greater than one, then the order in which each component is returned is PMU-model specific. Refer to the PMU specific man page. The raw encoding means the encoding as mandated by the underlying PMU model. It may not be directly suitable to pass to a kernel API. You may want to use API-specific library calls to ensure the correct encoding is passed. If fstr is not NULL, it will point to the fully qualified event string upon succesful return. The string contains the event name, any umask set, and the value of all the modifiers. It reflects what the encoding will actually measure. The function allocates the memory to store the string. The caller must eventually free the string. Here is a example of how this function could be used: #include <inttypes.h> #include <err.h> #include <perfmon/pfmlib.h> int main(int argc, char **argv) { uint64_t *codes 0; int count = 0; int ret; ret = pfm_initialize(); if (ret != PFMLIB_SUCCESS) err(1", cannot initialize library %s", pfm_strerror(ret)); ret = pfm_get_event_encoding("RETIRED_INSTRUCTIONS", PFM_PLM3, NULL, &codes, &count); if (ret != PFM_SUCCESS) err(1", cannot get encoding %s", pfm_strerror(ret)); for(i=0; i < count; i++) printf("count[%d]=0x%"PRIx64" ", i, codes[i]); free(codes); return 0; } RETURN
The function returns in *codes the encoding of the event and in *count the number of 64-bit integers to support that encoding. Upon suc- cess, PFM_SUCCESS is returned otherwise a specific error code is returned. ERRORS
PFM_ERR_TOOSMALL The code argument is too small for the encoding. PFM_ERR_INVAL The code or count argument is NULL. PFM_ERR_NOMEM Not enough memory. PFM_ERR_NOTFOUND Event not found. PFM_ERR_ATTR Invalid event attribute (unit mask or modifier) PFM_ERR_ATTR_VAL Invalid modifier value. PFM_ERR_ATTR_SET attribute already set, cannot be changed. PFM_ERR_ATTR_UMASK Missing unit mask. PFM_ERR_ATTR_FEATCOMB Unit masks or features cannot be combined into a single event. AUTHOR
Stephane Eranian <eranian@gmail.com> SEE ALSO
pfm_get_os_event_encoding(3) September, 2009 LIBPFM(3)
All times are GMT -4. The time now is 01:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy