1 | /* |
---|
2 | * submit_command_sync.c |
---|
3 | * |
---|
4 | * Created on: 2 juil. 2015 |
---|
5 | * Author: cresson |
---|
6 | */ |
---|
7 | |
---|
8 | #include "drmaa.h" |
---|
9 | #include <stdlib.h> /* exit, EXIT_FAILURE */ |
---|
10 | #include <stdlib.h> |
---|
11 | #include <string.h> |
---|
12 | |
---|
13 | void concat(char *s1, char *s2) |
---|
14 | { |
---|
15 | char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator |
---|
16 | //in real code you would check for errors in malloc here |
---|
17 | strcpy(result, s1); |
---|
18 | strcat(result, s2); |
---|
19 | free(s1); |
---|
20 | s1 = result; |
---|
21 | } |
---|
22 | |
---|
23 | int main (int argc, char **argv) { |
---|
24 | if (argc<2) |
---|
25 | { |
---|
26 | printf ("The given command is empty!\n"); |
---|
27 | return EXIT_FAILURE; |
---|
28 | } |
---|
29 | int rc = EXIT_SUCCESS; |
---|
30 | char error[DRMAA_ERROR_STRING_BUFFER]; |
---|
31 | int errnum = 0; |
---|
32 | drmaa_job_template_t *jt = NULL; |
---|
33 | |
---|
34 | // Init. drmaa session |
---|
35 | errnum = drmaa_init (NULL, error, DRMAA_ERROR_STRING_BUFFER); |
---|
36 | |
---|
37 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
38 | fprintf (stderr, "Could not initialize the DRMAA library: %s\n", error); |
---|
39 | return EXIT_FAILURE; |
---|
40 | } |
---|
41 | |
---|
42 | // Create a job template |
---|
43 | errnum = drmaa_allocate_job_template (&jt, error, DRMAA_ERROR_STRING_BUFFER); |
---|
44 | |
---|
45 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
46 | fprintf (stderr, "Could not create job template: %s\n", error); |
---|
47 | rc = EXIT_FAILURE; |
---|
48 | } |
---|
49 | else { |
---|
50 | |
---|
51 | // Setting job attributes |
---|
52 | int i; |
---|
53 | int count = 0; |
---|
54 | for (i=1; i<argc; i++) |
---|
55 | { |
---|
56 | count += strlen(argv[i]); // argument lenght |
---|
57 | count += 1; // space between arguments |
---|
58 | } |
---|
59 | char command[count]; |
---|
60 | strcpy(command, argv[1]); |
---|
61 | for (i=2; i<argc; i++) |
---|
62 | { |
---|
63 | strcat(command," "); |
---|
64 | strcat(command,argv[i]); |
---|
65 | } |
---|
66 | |
---|
67 | errnum = drmaa_set_attribute (jt, DRMAA_REMOTE_COMMAND, command /*"/gpfs-dell/data/work/GEOSUD/drmaa/test/drmaa_operator/bin/./sleeper.sh"*/, |
---|
68 | error, DRMAA_ERROR_STRING_BUFFER); |
---|
69 | |
---|
70 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
71 | fprintf (stderr, "Could not set attribute \"%s\": %s\n", |
---|
72 | DRMAA_REMOTE_COMMAND, error); |
---|
73 | rc = EXIT_FAILURE; |
---|
74 | } |
---|
75 | else { |
---|
76 | // Submit job |
---|
77 | char jobid[DRMAA_JOBNAME_BUFFER]; |
---|
78 | errnum = drmaa_run_job (jobid, DRMAA_JOBNAME_BUFFER, jt, error, |
---|
79 | DRMAA_ERROR_STRING_BUFFER); |
---|
80 | |
---|
81 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
82 | fprintf (stderr, "Could not submit job: %s\n", error); |
---|
83 | rc = EXIT_FAILURE; |
---|
84 | } |
---|
85 | else { |
---|
86 | printf ("Your job has been submitted with id %s\n", jobid); |
---|
87 | |
---|
88 | // wait job |
---|
89 | int status = 0; |
---|
90 | char jobid_out[DRMAA_JOBNAME_BUFFER]; |
---|
91 | drmaa_attr_values_t *rusage = NULL; |
---|
92 | errnum = drmaa_wait (jobid, jobid_out, DRMAA_JOBNAME_BUFFER, &status, |
---|
93 | DRMAA_TIMEOUT_WAIT_FOREVER, &rusage, error, |
---|
94 | DRMAA_ERROR_STRING_BUFFER); |
---|
95 | |
---|
96 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
97 | fprintf (stderr, "Could not wait for job: %s\n", error); |
---|
98 | } |
---|
99 | else { |
---|
100 | char usage[DRMAA_ERROR_STRING_BUFFER]; |
---|
101 | int aborted = 0; |
---|
102 | |
---|
103 | drmaa_wifaborted(&aborted, status, NULL, 0); |
---|
104 | |
---|
105 | if (aborted == 1) { |
---|
106 | printf("Job %s never ran\n", jobid); |
---|
107 | } |
---|
108 | else { |
---|
109 | int exited = 0; |
---|
110 | |
---|
111 | drmaa_wifexited(&exited, status, NULL, 0); |
---|
112 | |
---|
113 | if (exited == 1) { |
---|
114 | int exit_status = 0; |
---|
115 | |
---|
116 | drmaa_wexitstatus(&exit_status, status, NULL, 0); |
---|
117 | printf("Job %s finished regularly with exit status %d\n", jobid, exit_status); |
---|
118 | } |
---|
119 | else { |
---|
120 | int signaled = 0; |
---|
121 | |
---|
122 | drmaa_wifsignaled(&signaled, status, NULL, 0); |
---|
123 | |
---|
124 | if (signaled == 1) { |
---|
125 | char termsig[DRMAA_SIGNAL_BUFFER+1]; |
---|
126 | |
---|
127 | drmaa_wtermsig(termsig, DRMAA_SIGNAL_BUFFER, status, NULL, 0); |
---|
128 | printf("Job %s finished due to signal %s\n", jobid, termsig); |
---|
129 | } |
---|
130 | else { |
---|
131 | printf("Job %s finished with unclear conditions\n", jobid); |
---|
132 | } |
---|
133 | } /* else */ |
---|
134 | } /* else */ |
---|
135 | |
---|
136 | printf ("Job Usage:\n"); |
---|
137 | |
---|
138 | while (drmaa_get_next_attr_value (rusage, usage, DRMAA_ERROR_STRING_BUFFER) == DRMAA_ERRNO_SUCCESS) { |
---|
139 | printf (" %s\n", usage); |
---|
140 | } |
---|
141 | |
---|
142 | drmaa_release_attr_values (rusage); |
---|
143 | } |
---|
144 | } |
---|
145 | } /* else */ |
---|
146 | |
---|
147 | // Delete job template |
---|
148 | errnum = drmaa_delete_job_template (jt, error, DRMAA_ERROR_STRING_BUFFER); |
---|
149 | |
---|
150 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
151 | fprintf (stderr, "Could not delete job template: %s\n", error); |
---|
152 | return EXIT_FAILURE; |
---|
153 | } |
---|
154 | } /* else */ |
---|
155 | |
---|
156 | // Close drmaa session |
---|
157 | errnum = drmaa_exit (error, DRMAA_ERROR_STRING_BUFFER); |
---|
158 | |
---|
159 | if (errnum != DRMAA_ERRNO_SUCCESS) { |
---|
160 | fprintf (stderr, "Could not shut down the DRMAA library: %s\n", error); |
---|
161 | rc = EXIT_FAILURE; |
---|
162 | } |
---|
163 | |
---|
164 | return rc; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|